Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Regular Expression Basics : How many Matches?

A Regular Expression (regex) is a sequence of characters that define a pattern that allows you to search, match, locate, replace, manipulate, and manage text. I explained the basics of Regular Expressions in an earlier article: What is a Regular Expression?

In case you are not familiar with how to execute code like that in this article, you open a new text file (use a basic ASCII text editor like Windows Notepad, not a word processor. Word processors add formatting characters to the text). Paste the code into the text file. Save the text file with a name that has the file extension .htm. (say test.htm) Double-click on the file to open it in your web browser.

In this article you'll learn how to use regular expressions to count the number of matches. The example below will match the character a in the string "same sales sample".

<script>
var strTarget = "same sales sample";
var num = strTarget.match(/a/).length;
alert(num);
</script>

The code creates a text string "same sales sample" and then passes the regular expression /a/ to the JavaScript String Object method <i>match</i>. Even though we can see that there are 3 a's in the string, the code will return 1 because it will match only the first occurrence. To make it count all occurrences in the entire string we have to use the global identifier g, as shown below.

<script>
var strTarget = "same sales sample";
var num = strTarget.match(/a/g).length;
alert(num);
</script>

This time the code will return 3. We can search for more than single characters. The example below searches for occurrences if "ing" in the sentence "same sales sample".

<script>
var strTarget = "From beginning to ending it's exciting.";
var num = strTarget.match(/ing/g).length;
alert(num);
</script>

The code will return 3. However the code shown below will return 2.

<script>
var strTarget = "From beginning to ENDING it's exciting.";
var num = strTarget.match(/ing/g).length;
alert(num);
</script>

That's because by default the match is case sensitive. We solve that by adding the i switch to make the search case insensitive, as shown below.

<script>
var strTarget = "From beginning to ENDING it's exciting.";
var num = strTarget.match(/ing/ig).length;
alert(num);
</script>

Now let's suppose we want to count the number of "oo" character sequences in the string "look smooth soon". We could use the code shown below.

<script>
var strTarget = "look smooth soon";
var num = strTarget.match(/o/g).length;
alert(num);
</script>

The code will return 6. That's because it counted successive o's. One way to fix that is to use the + specifier as shown below.

<script>
var strTarget = "look smooth soon";
var num = strTarget.match(/o+/g).length;
alert(num);
</script>

The code will return 3. The + specifier means the character can occur one or more times in each group. So successive characters will not be counted.

<script>
var strTarget = "same sales sample";
var num = strTarget.match(/\s/g).length;
alert(num);
</script>

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro



Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2019 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268