Answers for "find all occurrences of a character in a string javascript"

8

count occurrences of character in string javascript

var temp = "This is a string.";
var count = (temp.match(/is/g) || []).length;
console.log(count);

Output: 2

Explaination : The g in the regular expression (short for global) says to search the whole string rather than just find the first occurrence. This matches 'is' twice.
Posted by: Guest on March-04-2020
31

javascript replace

var res = str.replace("find", "replace");
Posted by: Guest on October-21-2019
0

javascript find all occurrences in string

var str = "I learned to play the Ukulele in Lebanon."
var regex = /le/gi, result, indices = [];
while ( (result = regex.exec(str)) ) {
    indices.push(result.index);
}
console.log(indices) // => [2, 25, 27, 33]
//find all occurence of le and return the return an array of the indeces
Posted by: Guest on May-11-2021

Code answers related to "find all occurrences of a character in a string javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language