Regular Expressions Intervals
By Stephen Bucaro
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?
Intervals tell us about the number of occurrences of a character in a string.
Curly braces are used to define a precise count of how many occurrences you
are checking for.
Expressions Intervals Syntax
{n} | Matches the preceding character exactly n times |
{n,m} | Matches the preceding character a minimum of n times and a maximim of m times |
{n,} | Matches the preceding character n times or more |
{,n} | Matches the preceding character 0 times or more |
The example shown below checks for occurrences of two lower-case s in the string
"Stressed is desserts spelled backwards."
<script>
let strTarget = "Stressed is desserts spelled backwards.";
let regexp = /s{2}/g;
let res = strTarget.match(regexp);
alert(res.length);
</script>
The expression s{2} defines to match lower case s where it appears exactly 2 times.
The /g controls specifies to search the entire string for occurrences. The
match .method searches the string for a match against a regular expression,
and returns the matches, as an Array object. The array .length property
is displayed by a message box.
This example displays the number 2 in the message box, indicating that it
found 2 matches with ss in the string.
The example shown below matches occurrences of lower case s where they appear 1
time or 2 times in the string "Stressed is desserts ssspelled backwardss.",
but not where they appear more than 2 times.
<script>
let strTarget = "Stressed is desserts ssspelled backwardss.";
let regexp = /s{1,2}/g;
let res = strTarget.match(regexp);
alert(res);
</script>
The expression s{1,2} defines to match lower case s where it appears 1
time or 2 times.
This example displays ss,s,ss,s,ss,s,ss in the message box.
The example shown below matches occurrences of lower case s where they appear 2
or more times in the string "Stressed is desserts ssspelled backwardss."
<script>
let strTarget = "Stressed is desserts ssspelled backwardss.";
let regexp = /s{2,}/g;
let res = strTarget.match(regexp);
alert(res);
</script>
The expression s{2,} defines to match lower case s where it appears 2 times or more.
This example displays ss,ss,sss,ss in the message box.
The example shown below matches occurrences of lower case s where it appears 2
or more times in the string "treed desert pelled backward."
<script>
let strTarget = "treed deert pelled backward.";
let regexp = /s{2}/g;
let res = strTarget.match(regexp);
if(res === null) alert(0);
else alert(res.length);
</script>
Because the string does not contain an occurrence where they lower case s appears 2
or more times, the .match method will return null. The code if(res === null) tests
for the null value, and if found displays 0 in the message box.
Note: the === comparison operator tests both the value and the type of the value,
so this test will pass ONLY for null, not pass for "", undefined, false, 0, or NaN.
You might want to test the example by changing the word "deert" to "dessert". Then
the message box will display 1. This testing for the null value is useful for
many regular expression code statements.
More Java Script Code: • JavaScript Code to Display Wait Cursor While Image Loads • JavaScript to Replace All Occurrences of a Substring Within a String • Easy JavaScript Picture Selector Code • Easy HTML5 Drag and Drop • Easy Java Script Animation • Java Script to Dynamically Add, Remove, and Change Items in a Select List • Java Script Comparison Operators • What is a Regular Expression? • A Cross-Browser Solution for Handling JavaScript Events • Easy Picture Zoom Code
|