jQuery String in String Counting

Share this article

How to count the number of occurences of a string (subset of characters/regular expression/wildcards) inside another string.

Method 1 – Using a Regular Expression Match

var emails = 'sam@jquery4u.com,admin@jquery4u.com,someone@jquery4u.com',
    count = emails.match(/@/igm).length;
console.log(count);
//output: 3
Word of warning that using .length function on a regular expression which has no matches will result in a null error. TypeErroremailsmatch
var emails = '',
    count = emails.match(/@/igm).length;
console.log(count);
//output: TypeError: emails.match(/@/gim) is null
If you check the count is not null before assigning the value it does not error and will give you 0 for a no count. Working Version (null proof):
var emails = 'sam@jquery4u.com,admin@jquery4u.com,someone@jquery4u.com',
    regex = /@/igm,
    count = emails.match(regex),
    count = (count) ? count.length : 0;

console.log(count);
//output: 3

Method 2 – Using a indexOf() Pos Function

function occurrences(string, substring){
    var n=0;
    var pos=0;

    while(true){
        pos=string.indexOf(substring,pos);
        if(pos!=-1){ n++; pos+=substring.length;}
        else{break;}
    }
    return(n);
}
count= occurrences(emails,'@');
console.log(count);
//output: 3

Frequently Asked Questions (FAQs) about jQuery String Counting

How can I count the number of characters in a string using jQuery?

To count the number of characters in a string using jQuery, you can use the .length property. This property returns the number of characters in a string. Here’s an example:

var str = "Hello World!";
var n = str.length;
In this example, n will be 12, because “Hello World!” is 12 characters long, including the space and the exclamation point.

How can I count the number of occurrences of a specific character in a string using jQuery?

To count the number of occurrences of a specific character in a string, you can use the .split() method in combination with the .length property. Here’s an example:

var str = "Hello World!";
var count = (str.split("o").length - 1);
In this example, count will be 2, because the letter “o” appears twice in “Hello World!”.

How can I count the number of words in a string using jQuery?

To count the number of words in a string, you can use the .split() method with a space as the separator, and then use the .length property. Here’s an example:

var str = "Hello World!";
var words = str.split(" ").length;
In this example, words will be 2, because “Hello World!” contains two words.

How can I find the length of a string without using the .length property in jQuery?

If you want to find the length of a string without using the .length property, you can use a loop to iterate through the string until it reaches the end. Here’s an example:

var str = "Hello World!";
var length = 0;
while (str[length] !== undefined)
length++;
In this example, length will be 12, because “Hello World!” is 12 characters long.

How can I count the number of occurrences of a specific word in a string using jQuery?

To count the number of occurrences of a specific word in a string, you can use the .split() method with the word as the separator, and then use the .length property. Here’s an example:

var str = "Hello World! Hello again!";
var count = (str.split("Hello").length - 1);
In this example, count will be 2, because the word “Hello” appears twice in the string.

How can I count the number of sentences in a string using jQuery?

To count the number of sentences in a string, you can use the .split() method with a period, exclamation point, or question mark as the separator, and then use the .length property. Here’s an example:

var str = "Hello World! How are you? I'm fine.";
var sentences = str.split(/[.!?]/).length - 1;
In this example, sentences will be 3, because the string contains three sentences.

How can I count the number of paragraphs in a string using jQuery?

To count the number of paragraphs in a string, you can use the .split() method with two newline characters as the separator, and then use the .length property. Here’s an example:

var str = "Hello World!\n\nHow are you?\n\nI'm fine.";
var paragraphs = str.split("\n\n").length;
In this example, paragraphs will be 3, because the string contains three paragraphs.

How can I count the number of lines in a string using jQuery?

To count the number of lines in a string, you can use the .split() method with a newline character as the separator, and then use the .length property. Here’s an example:

var str = "Hello World!\nHow are you?\nI'm fine.";
var lines = str.split("\n").length;
In this example, lines will be 3, because the string contains three lines.

How can I count the number of vowels in a string using jQuery?

To count the number of vowels in a string, you can use the .match() method with a regular expression that matches vowels, and then use the .length property. Here’s an example:

var str = "Hello World!";
var vowels = (str.match(/[aeiou]/gi) || []).length;
In this example, vowels will be 3, because “Hello World!” contains three vowels.

How can I count the number of consonants in a string using jQuery?

To count the number of consonants in a string, you can use the .match() method with a regular expression that matches consonants, and then use the .length property. Here’s an example:

var str = "Hello World!";
var consonants = (str.match(/[bcdfghjklmnpqrstvwxyz]/gi) || []).length;
In this example, consonants will be 7, because “Hello World!” contains seven consonants.

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

jQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form