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 Expressions Intervals

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:
• Replace Drop-down with Drag-and-drop
• Regular Expression Position Matching
• Object-Oriented JavaScript
• Calculators for Your Web Site : Body Mass Index
• Basic State Machines
• Easy Java Script Code for Sideways Slide show
• Date Picker For Your Web Site
• JavaScript to Add and Remove Rows and Cells from a Table
• How to Disable the Browser Back Button
• Code for a Less annoying Popup Window

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-2024 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268