Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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.
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #count #occurrences #character #string #javascript
ADD COMMENT
Topic
Name
3+2 =