We can use JavaScript to count the occurrences of a char or number in a string by using the JavaScript match() method, the length property, and a regex expression.
someString.match(/c/gi).length;
Not that the formula above works great as long as the character or number is in the string. However, if it is not found in the string, the expressions will return an error. This is because someString.match(/c/gi)
will return null if c is not found in the string, and you cannot use the length property of a null value.
To make sure our code always works, we can simply add an if-else statement to check for this.
var charOccurrences = string1.match(/c/gi);
if( charOccurrences != null ){
charOccurrences = charOccurrences.length;
} else {
charOccurrences = 0;
}
Let’s see this in action with the following example.
var string1 = "How many times does c appear in this sentence?";
var numOfOccurrences = string1.match(/c/gi);
if( numOfOccurrences != null ){
var numOfOccurrences = numOfOccurrences.length;
} else {
var numOfOccurrences = 0;
}
console.log(numOfOccurrences);
#Output
2
Count the Occurrences in a String Using JavaScript
In this example, we will let the user enter a string and a char or number they want to count the number of occurrences of.
First, we will set up some HTML that will let a user enter a string, and a value. Then we will use a function to count the number of times each value appears in the String.
Here is the HTML:
Below is a function that will get the count the occurrences of the user value in a given string. We will then post the results using the textContent property.
function findOccurrences(){
//Get the string the user has entered
var string = document.getElementById("string1").value;
//Get value user has entered
var val1 = document.getElementById("string2").value;
//Count how many values are in the string
var regex = new RegExp(val1,"gi");
var count = string.match(regex);
if( count != null ){
count = count.length;
} else {
count = 0;
}
//Post the number of occurrences for user to see
document.getElementById("userString").textContent = string;
document.getElementById("userValue").textContent = val1;
document.getElementById("numOccurrences").textContent = count;
};
The final code and output for counting the number of occurrences in a String is below:
Code Output:
Full Code:
function findOccurrences(){ var string = document.getElementById("string1").value; var val1 = document.getElementById("string2").value; var regex = new RegExp(val1,"gi"); var count = string.match(regex); if( count != null ){ count = count.length; } else { count = 0; } document.getElementById("userString").textContent = string; document.getElementById("userValue").textContent = val1; document.getElementById("numOccurrences").textContent = count; };
Count Occurrences in a String Using a For Loop
We can also easily count the occurrences of a number in a String using a loop and counting the number of occurrences we find in the String.
var string1 = "How 2many times2 the number 2 occurs in this sentence2?";
function countOccurrences(string){
var theString = string.toLowerCase();
var char;
var count = 0;
for ( var i = 0; i < theString.length; i++ ){
char = theString.charAt(i);
if (char == 2){
count = count + 1;
};
};
return count;
};
var numOfOccurrences = countOccurrences(string1);
The result of the above example (numOfOccurrences) would return 4, as the number 2 occurs 4 times in the sentence above.
Hopefully this article has been useful for you to learn how to use JavaScript to count the occurrences in string.