How to usesplit() function in Javascript
split() Method is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument.
Example: Counting occurrence of Geeks in “w3wiki” using split() function.
function gfg() {
let s = "Geeks for Geeks";
let f = "Geeks";
let r = s.split(f).length - 1;
console.log(r);
}
gfg()
Output
2
How to count string occurrence in string using JavaScript ?
In JavaScript, we can count the string occurrence in a string by counting the number of times the string is present in the string.
This can be done in the following ways:
Table of Content
- Approach 1: Using match() function
- Approach 2: Using a loop
- Approach 3: Using split() function
- Approach 4: Using Indexof()
- Approach 5: Using regular expressions
Contact Us