Illustrations of Regular Expressions in Core JavaScript



Navigation Aids -- This Page           Navigation Aids -- This Topic           Navigation Aids -- This Site








Table of Contents -- This Page

Our illustrations will proceed from the simple concepts to the more difficult. Each step will be illustrated with code examples and an explanation. We provide links to supporting topics. A outline of each step follows:

  1. Create a Simple RegExp Object
  2. Perform a Simple Pattern Match
  3. Perform a Global Search (The Global Flag)
  4. Example of RegExp Literal
  5. A Simple Character Class
  6. A Negation Class
  7. A Range Class
  8. The Predefined Digits Class
  9. The Predefined Non-Whitespace Class
  10. The Predefined Word Class
  11. The Wildcard
  12. Greedy Quantifier
  13. Reluctant Quantifier
  14. Possessive Quantifier
  15. Grouping Patterns
  16. Backreference Patterns
  17. Non-Capturing Group Patterns
  18. Alternation Patterns
  19. Anchor Patterns
  20. Assertion Patterns
  21. Boundary Patterns
  22. Practical Applications:
  23. Match a Phone Number
  24. Match an E-mail Address
  25. Match Day in dd/mm/ccyy Format
  26. Match Month in dd/mm/ccyy Format
  27. Match Year in dd/mm/ccyy Format
  28. Complete Pattern For European Format: dd/mm/ccyy




A Step By Step Approach


1. Create a Simple RegExp Object

// Note 01 var reDog = new RegExp("dog");// Note 02 // Note 03
Note 01:
Before we can consider performing pattern matching with a string, we first need a regexp pattern.
Note 02:
Our regexp pattern in created with the RegExp constructor. Our constructor builds us our RegExp object. This object instance is called "reDog". Henceforth, all of our RegExp objects will begin with "re".
Note 03:
Our reDog object has a very simple pattern. The pattern of reDog will match only the first occurrence of the word "dog" in a string.



2. Perform a Simple Pattern Match

// Note 01 var dogBytes = "A dog bites"; var reDog = new RegExp("dog"); reDog.test(dogBytes);// Note 02 // Note 03
Note 01:
First we build the target string for our illustration.
Note 02:
We use the test() method of the regexp object reDog. The method will return "true".
Note 03:
In JavaScript, regular expressions are facilitated through the methods of the RegExp native class. We used the test() method above, but we could have used other method options. The String class also contains methods designed to work with regular expressions. The next example will use the match() method of the String class to perform the operation.



3. Perform a Global Search (The Global Flag)

// Note 01 var dogBytes = "A dog bites another dog"; var reDog = new RegExp("dog", "g");// Note 02 dogBytes.match(reDog);// Note 03
Note 01:
Our target string, "dogBytes", was modified to show two occurrences of "dog".
Note 02:
Our constructor is modified to accept a second argument. The second argument may be one or more flags. Our flag is "g" which indicates a global search of the target string.
Note 03:
This time we use the match() method of the string object dogBytes. The method will return an array with the results of the match. Our global search returned two array elements each being a string that contains "dog".



4. Example of RegExp Literal

This example is identical to the above example but instead of using the RegExp constructor to create a RegExp instance, we use the regexp literal to create the instance.

var dogBytes = "A dog bites another dog"; var reDog = /dog/g; // Note 01 dogBytes.match(reDog);//
Note 01:
A regexp literal is enclosed by forward slash characters. The JavaScript interpreter recognizes this regexp literal format and will associate the variable "reDog" with a status given to a RegExp object. Our global flag "g" follows the literal but is not part of the literal.



5. A Simple Character Class

See the definition for a simple character class.

var sString = "3rd, 4th, 5th, 6th, 7th, 8th"; var reObj = /[4567]th/g; //a literal with a simple character class sString.match(reObj);//using the String class method for the match   /[4567]th/g; 0000000001111111111222222 1234567890123456789012345
The parts of the above regexp pattern
cols chars syntax Purpose
matches: 4th, 5th, 6th, 7th
01&10 / / literal meta chars syntax for enclosing a regexp literal
02&07 [ ] character class meta chars syntax for enclosing a character class
03-06 4567 literal characters the class of characters
08-09 th literal characters part of the matching criteria
11 g flag global flag; find all matches

Illustration Explanation: the simple character class is designated with square brackets. The characters that are within the character class brackets will be included in the regexp matching pattern. In our case, characters 4 5, 6 and 7 are included in the matching pattern. This character set is combined with the literal characters "th" to form the complete matching pattern. Because the global flag is included, the matching process will continue until the entire string has been searched. The "match" method of the String class will return an array of all matching occurrences (separated by commas).




6. A Negation Class

See the definition for a negation class.

var sString = "3rd, 4th, 5th, 6th, 7th, 8th"; var reObj = /[^4567]th/g; //a literal with a negation class sString.match(reObj);//using the String class method for the match   /[^4567]th/g; 0000000001111111111222222 1234567890123456789012345
The parts of the above regexp pattern
cols chars syntax Purpose
matches: 8th
01&11 / / literal meta chars syntax for enclosing a regexp literal
02&08 [ ] character class meta chars syntax for enclosing a character class
03 ^ negation class meta chars syntax for designating a negation class
04-07 4567 literal characters the negated class of characters
09-10 th literal characters part of the matching criteria
12 g flag global flag; find all matches

Illustration Explanation: the negation version of the character class is designated with the caret. The characters that follow the caret but within the character class will be excluded from the regexp matching pattern. In our case, characters 4 5, 6 and 7 are excluded from the pattern and all remaining characters of the literal character set are included. This character set is combined with the literal characters "th" to form the matching pattern. Because the global flag is included, the matching process will continue until the entire string has been searched. The "match" method of the String class will return an array of all matching occurrences (separated by commas).




7. A Range Class

See the definition for a range class.

var sString = "3rd, 4th, 5th, 6th, 7th, 8th"; var reObj = /[5-7]th/g; //a literal with a range class sString.match(reObj);//using the String class method for the match   /[5-7]th/g; 0000000001111111111222222 1234567890123456789012345
The parts of the above regexp pattern
cols chars syntax Purpose
matches: 5th, 6th, 7th
01&09 / / literal meta chars syntax for enclosing a regexp literal
02&06 [ ] character class meta chars syntax for enclosing a character class
03&05 57 literal characters the class of characters: 5,6,7
04 - range class meta chars syntax for designating a range class
07-08 th literal characters part of the matching criteria
10 g flag global flag; find all matches

Illustration Explanation: the range version of the character class is designated with the hyphen. The characters on each side of the hyphen (5 and 7) indicate the upper and lower range limit. In our case the range is 5, 6 and 7. This class of characters (5 - 7) is combined with the literal characters "th" to form the matching pattern. Because the global flag is included, the matching process will continue until the entire string has been searched. The "match" method of the String class will return an array of all matching occurrences (separated by commas).




8. The Predefined Digits Class

See the definition for a predefined class.

var sString = "3rd, 4th, 5th, 6th, 7th, 8th"; var reObj = /[\d]th/g; //a literal with a predefined digit class sString.match(reObj);//using the String class method for the match   /[\d]th/g; 0000000001111111111222222 1234567890123456789012345
The parts of the above regexp pattern
cols chars syntax Purpose
matches: 4th, 5th, 6th, 7th, 8th
/[\D]th/g; the same regexp with non-digit (\D-uppercase) returns null
01&08 / / literal meta chars syntax for enclosing a regexp literal
02&05 [ ] character class meta chars syntax for enclosing a character class
03&04 \d a token for a predefined class syntax for designating the digits class
06-07 th literal characters part of the matching criteria
09 g flag global flag; find all matches

Illustration Explanation: the predefined class of "digits" is represented by the token "\d". This class of characters (0 - 9) is combined with the literal characters "th" to form the matching pattern. Because the global flag is included, the matching process will continue until the entire string has been searched. The "match" method of the String class will return an array of all matching occurrences (separated by commas).




9. The Predefined Non-Whitespace Class

See the definition for a predefined class.

var sString = "3rd, 4th, 5th, 6th, 7th, 8th"; var reObj = /[\S]th/g; //a literal with a non-whitespace class sString.match(reObj);//using the String class method for the match   /[\S]th/g; 0000000001111111111222222 1234567890123456789012345
The parts of the above regexp pattern
cols chars syntax Purpose
matches: 4th, 5th, 6th, 7th, 8th
/[\s]th/g; the same regexp with whitespace (\s-lowercase) returns null
01&08 / / literal meta chars syntax for enclosing a regexp literal
02&05 [ ] character class meta chars syntax for enclosing a character class
03&04 \S a token for a predefined class syntax for designating the non-whitespace class
06-07 th literal characters part of the matching criteria
09 g flag global flag; find all matches

Illustration Explanation: the predefined class of non-whitespace (all characters except whitespace) is represented by the non-whitespace token "\S". This class of characters (non-whitespace) is combined with the literal characters "th" to form the matching pattern. Because the global flag is included, the matching process will continue until the entire string has been searched. The "match" method of the String class will return an array of all matching occurrences (separated by commas).




10. The Predefined Word Class

See the definition for a predefined class.

var sString = "3rd, 4th, 5th, 6th, 7th, 8th"; var reObj = /[\w]/g; //a literal with a word class sString.match(reObj);//using the String class method for the match   /[\w]/g; 0000000001111111111222222 1234567890123456789012345
The parts of the above regexp pattern
cols chars syntax Purpose
matches: 3,r,d,4,t,h,5,t,h,6,t,h,7,t,h,8,t,h
/[\W]/g; the same regexp with non-word (\W-uppercase) returns null
01&06 / / literal meta chars syntax for enclosing a regexp literal
02&05 [ ] character class meta chars syntax for enclosing a character class
03&04 \w a token for a predefined class syntax for designating the word class
07 g flag global flag; find all matches

Illustration Explanation: the predefined class of "\w" is the only criteria for the matching pattern. So, "\w" matches all the chars in the target string except the space(s) and the commas. The "match" method of the String class will return an array of all matching occurrences (separated by commas).




11. The Wildcard

See the definition for a wildcard predefined character class.

var sString = "aweb ofweb theweb 123"; var reObj = /..web/g; //a literal with the wildcard period sString.match(reObj);//using the String class method for the match   /..web/g; 0000000001111111111222222 1234567890123456789012345
The parts of the above regexp pattern
cols chars syntax Purpose
matches: ofweb,heweb
aweb did not produce a match since the pattern has two wildcards
01&07 / / literal meta chars syntax for enclosing a regexp literal
02&03 •• wildcard meta chars syntax for designating a wildcard
04-06 web literal characters part of the matching criteria
08 g flag global flag; find all matches

Illustration Explanation: the pattern contains two wildcard characters that are to be combined with the literal characters "web". The wildcard tokens will match to all possible literal characters (except \n and \r) that are in the target string. If the target string contains two qualifying characters before a "web" occurrence, then we have a match. Because the global flag is included, the matching process will continue until the entire string has been searched.The "match" method of the String class will return an array of all matching occurrences (separated by commas).




12. Greedy Quantifier

See the definition for a greedy quantifier.

var sString = "aweb ofweb theweb 123"; var reObj = /.*web/; //a literal with the wildcard and quantifier sString.match(reObj);//using the String class method for the match   /.*web/; 0000000001111111111222222 1234567890123456789012345
The parts of the above regexp pattern
cols chars syntax Purpose
matches: aweb ofweb theweb
note: there are no commas, only one match was returned here
01&07 / / literal meta chars syntax for enclosing a regexp literal
02 wildcard meta chars syntax for designating a wildcard
03 * quantifier meta chars syntax for designating a zero or more occurs
04-06 web literal characters part of the matching criteria

Search Explanation: Here is how the greedy search works using the test() method:

  1. The greedy search starts at the end character (rightmost) of the target string and uses all characters of the target string. The first attempt at a match looks like: reObj.test("aweb ofweb theweb 123");// returns false
  2. Greedy eliminates the last character of the target string and matches to same pattern again. reObj.test("aweb ofweb theweb 12");// returns false
  3. The repetition (match attempt, no match, eliminate a character, match attempt, .....) continues until there is a match or until the target string is exhausted of characters. We occur a match on the fifth attempt: reObj.test("aweb ofweb theweb");// returns true

The pattern: /.*web/; contains one wildcard and a quantifier. The quantifier "*" applies to the wildcard ".". So, ".*" is responsible for matching the "aweb ofweb the" portion of the string. The literal characters "web" is the other portion of the regexp. The wildcard and the quantifier characters are combined with the literal characters "web" to form the complete regexp pattern. The wildcard tokens will match to all possible literal characters (except \n and \r) that are in the target string. The "*" quantifier indicates "zero or more occurrences". This is why ".*" returns all characters to the left of the first "web" occurrence (the first occurrence coming from the end or right of the string).




13. Reluctant Quantifier

See the definition for a reluctant quantifier.

var sString = "aweb ofweb theweb 123"; var reObj = /.*?web/; //literal has a wildcard and two quantifiers sString.match(reObj);//using the String class method for the match   /.*?web/; 0000000001111111111222222 1234567890123456789012345
The parts of the above regexp pattern
cols chars syntax Purpose
matches: aweb
01&08 / / literal meta chars syntax for enclosing a regexp literal
02 wildcard meta chars syntax for designating a wildcard
03 * quantifier meta chars syntax for designating a zero or more occurs
04 ? quantifier meta chars syntax for designating a reluctant search
05-07 web literal characters part of the matching criteria

Search Explanation: Here is how the reluctant search works using the test() method:

  1. The reluctant search starts at the first character (leftmost) of the target string and uses only that one character for the initial search. reObj.test("a");// returns false
  2. Reluctant adds the next character of the target string and matches to same pattern again. reObj.test("aw");// returns false
  3. The repetition (match attempt, no match, add a character, match attempt, .....) continues until there is a match or until the target string is fully represented. We occur a match on the fourth attempt: reObj.test("aweb");// returns true

The pattern: /.*?web/; contains one wildcard and a two quantifiers. The wildcard token (the period) will match all possible literal characters (except \n and \r) that are in the target string. The quantifiers "*?" apply to the wildcard. So, ".*?" is responsible for matching the "a" portion of the string. The literal characters "web" is the other portion of the regexp pattern. The wildcard and the quantifier characters are combined with the literal characters "web" to form the complete regexp pattern. The "*" quantifier indicates "zero or more occurrences". So ".*" would return any and all characters to the left of the first "web" occurrence The reluctant quantifier "?" basically determines how the search is conducted (it changes a greedy search to a reluctant search).

If the pattern had been changed to include the global flag: var reObj = /.*?web/g; then the reluctant search would not have stopped with the first match but continued to return: aweb, ofweb, theweb




14. Possessive Quantifier

See the definition for a possessive quantifier.

var sString = "aweb ofweb theweb 123"; var reObj = /.*+web/; //literal has a wildcard and two quantifiers sString.match(reObj);//using the String class method for the match   /.*+web/; 0000000001111111111222222 1234567890123456789012345
The parts of the above regexp pattern
cols chars syntax Purpose
returns null (no match)
01&08 / / literal meta chars syntax for enclosing a regexp literal
02 wildcard meta chars syntax for designating a wildcard
03 * quantifier meta chars syntax for designating a zero or more occurs
04 + quantifier meta chars syntax for designating a possessive search
05-07 web literal characters part of the matching criteria

Search Explanation: Here is how the possessive search works using the test() method:

  1. The possessive search is in fact not a search. The possessive quantifier attempts one match and one match only. That one and only match attempt will look like: reObj.test("aweb ofweb theweb 123");// returns false
  2. A string that looks like the following would have returned true: reObj.test("aweb ofweb theweb");// returns true

Because possessive quantifiers only do one test, when that test fails, you are done. There is no looping and a failed match gives no result. The " 123" at the end of the string caused the match to fail.




15. Grouping Patterns

See the definition for a grouping patterns.

var sString1 = "you bad bad bad bad boy"; var reObj = /(bad ){2,4}boy/g; sString1.match(reObj);   /(bad ){2,4}boy/g; 0000000001111111111222222 1234567890123456789012345
The parts of the above regexp pattern
cols chars syntax Purpose
returns: bad bad bad bad boy
01&16 / / literal meta chars syntax for enclosing a regexp literal
02&07 ( ) grouping meta chars syntax for designating a group
03-06 bad literal chars the grouping
08&12 { } repetition quantifier meta chars syntax for designating the repetitions
08-12 {2,4} the repetition criteria match item a minimum of 2 times
but no more than 4 times
13-15 boy literal characters part of the matching criteria

The grouping is the literal characters "bad", plus a space. They are contained within the parentheses which are the meta characters that indicate a group. The repetition criteria "{2,4}" are qualifiers that will control matching of the previous item (the group). The qualifiers say; the item "bad" must match at least two times but not more than four times. The literal characters "boy" is a simple pattern that is appended to the former pattern (but not affected by the repetition qualifiers). Our string has four occurs of "bad" giving a result of "bad bad bad bad boy". The group meta characters were expressed for capturing the group (as opposed to non-capturing).




16. Backreference Patterns

See the definition for a backreference patterns.

var sString1 = "boyboyboy"; var reObj = /(boy)\1\1/g;// the pattern becomes boyboyboy sString1.match(reObj);   /(boy)\1\1/g; 0000000001111111111222222 1234567890123456789012345
The parts of the above regexp pattern
cols chars syntax Purpose
returns: boyboyboy
01&11 / / literal meta chars syntax for enclosing a regexp literal
02&06 ( ) grouping meta chars syntax for designating a group
03-05 boy literal chars the grouping
07-08 \1 backreference syntax refers back to "boy"
09-10 \1 backreference syntax refers back to "boy"
12 g flag global flag; find all matches

A reference always is associated with a grouping. The grouping is the literal characters "boy". There are two backreference notations in the pattern; each being "\1". The "\1" denotes that we are referring to the first group in the pattern. Both equate to "boy", making the complete pattern equal to "boyboyboy". Our target string has "boyboyboy" occurring in it's character set therefore, we have a match.




17. Non-Capturing Group Patterns

See the definition for a non-capturing patterns.

var sString1 = "boyboyboy"; var reObj = /(?:boy)/; sString1.match(reObj);   /(?:boy)/; 0000000001111111111222222 1234567890123456789012345
The parts of the above regexp pattern
cols chars syntax Purpose
returns: boy
01&09 / / literal meta chars syntax for enclosing a regexp literal
02&08 ( ) grouping meta chars syntax for designating a group
03&04 ?: non-capturing meta chars do not remember group match
05-07 boy literal chars the grouping

The normal syntax for a group also implies the characters of the target string will be remembered (captured) for later reference. If backreferencing is not important in the desired pattern matching, then it is advantageous to turn off the reference capture. The syntax for a non-capturing group must contain the meta characters "?:". It is within the parentheses but before the grouping pattern.

If the pattern had been changed to include the global flag: var reObj = /(?:boy)/g; then the non-captuing search would not have stopped with the first match but continued to return: boy,boy,boy

If the pattern had been changed to include a backreference: var reObj = /(?:boy)\1/;// a backreference for a non-capturing group is illogical then the non-captuing search would get confused and return: null




18. Alternation Patterns

See the definition for a alternation patterns.

var sString1 = "first Street 1st Street"; var reObj = /(First|first|1st) Street/g; sString1.match(reObj);   /(First|first|1st) Street/g; 000000000111111111122222222 123456789012345678901234567
The parts of the above regexp pattern
cols chars syntax Purpose
returns: first Street,1st Street
01&26 / / literal meta chars syntax for enclosing a regexp literal
02&18 ( ) grouping meta chars syntax for designating a group
08&14 | alternation meta chars syntax for designating an alternation
03-07
09-13
15-17
First
first
1st
literal chars the alternatives
19-25 Street literal characters part of the matching criteria (initial blank too)
27 g flag global flag; find all matches

This pattern suggests three alternative sub patterns that can be combined with "Street" to complete the matching criteria. A target string with either "First", "first" or "1st" will satisfy the matching criteria given that "Street" is also satisfactory placed in the target string. Our target string produced two matches since we used the global flag in our regexp.




19. Anchor Patterns

See the definition for a anchors.

var sString3 = "Javascript: the client tool"; var reObj = /^javascript/i; sString3.match(reObj);   /^javascript/i; 000000000111111111122222222 123456789012345678901234567
The parts of the above regexp pattern
cols chars syntax Purpose
returns: Javascript
01&13 / / literal meta chars syntax for enclosing a regexp literal
02 ^ anchor meta chars syntax to anchor at beginning of string
03-12 javascript literal characters the matching criteria
14 i flag case insensitivity flag

This pattern anchors the regexp interpreter to the beginning of the target string. The regexp search criteria is successful when the pattern matches the beginning of the string. The caret is the meta character that determines the beginning of the string anchor. To match the end of the above string, use the following regexp pattern with the dollar sign. var reObj = /tool$/;




20. Assertion Patterns

See the definition for a assertion.

var sString3 = "Java Javascript Javacode VBscript"; var reObj = /Java(?=script)/g; sString3.match(reObj);   /Java(?=script)/g; 000000000111111111122222222 123456789012345678901234567
The parts of the above regexp pattern
cols chars syntax Purpose
returns: Java
01&16 / / literal meta chars syntax for enclosing a regexp literal
02-05 Java literal characters the matching criteria
06&15 ( ) assertion meta chars syntax to embrace the assertion
07-08 ?= positive assertion meta chars specify a positive assertion
09-14 script literal characters the assertion
17 g flag global search flag

This pattern contains a type of anchor called an assertion. This example has a positive assertion. The assertion is the literal characters "script". And it specifies that the match with "Java" will not occur unless "script" immediately follows. Notice that there was only one match with the above target string and that "Java" was returned. The assertion is not returned.

A negative assertion with the same string could look like: /Java(?!script)/g; This pattern would find two matches with the above string and return: "Java,Java". This negative assertion matched with "Java" and "Javacode" in the above string. It could not match with "Javascript".




21. Boundary Patterns

See the definition for a boundary.

var sString3 = "Javascript Java InstantJava"; var reObj = /\bJava\b/g; sString3.match(reObj);   /\bJava\b/g; 000000000111111111122222222 123456789012345678901234567
The parts of the above regexp pattern
cols chars syntax Purpose
returns: Java
01&10 / / literal meta chars syntax for enclosing a regexp literal
02-03 \b word boundary token anchor at beginning of word boundary
04-07 Java literal characters the matching criteria
08-09 \b word boundary token anchor at end of word boundary
11 g flag global search flag

This pattern contains a type of anchor called a word boundary. In fact there are two word boundary tokens; one at the beginning of the word "Java" and another at the end. With a word boundary, it is implied that a non-word character must exist. In our example, the non-word character is the space. So, we have a space before and after one of the words in our target string. This is the standalone word "Java". The words "Javascript" and "InstantJava" in our target string did not match the pattern "/\bJava\b/".




22. Match a Phone Number

var sPhone = "(123) 123-4567"; var reObj = /^\(\d{3}\) \d{3}-\d{4}$/; sPhone.match(reObj);   /^\(\d{3}\) \d{3}-\d{4}$/; 000000000111111111122222222 123456789012345678901234567
The parts of the above regexp pattern
cols chars syntax Purpose
returns: (123) 123-4567
01&25 / / literal meta chars syntax for enclosing a regexp literal
02 ^ anchor meta chars syntax to anchor at beginning of string
03-04 \( escape a meta chars the forward slash escapes the left parenthesis
the left parenthesis is a valid regexp meta character;
therefore, it must be escaped
05-06 \d a token for a predefined class syntax for designating the digits class
07&09 { } repetition quantifier meta chars syntax for designating a repetition
07-09 {3} the repetition criteria match the previous pattern exactly 3 times
the previous pattern is the predefined digits class
therefore, we need exactly three digits
10-11 \) escape a meta chars the forward slash escapes the right parenthesis
the right parenthesis is a regexp meta character;
therefore, it must be escaped
12 space space is a non-word char part of the matching criteria
13-14 \d a token for a predefined class syntax for designating the digits class
15-17 {3} the repetition criteria match the previous pattern exactly 3 times
the previous pattern is the predefined digits class
therefore, we need exactly three digits
18 hyphen hyphen is a non-word char part of the matching criteria
19-20 \d a token for a predefined class syntax for designating the digits class
21-23 {4} the repetition criteria match the previous pattern exactly 4 times
the previous pattern is the predefined digits class
therefore, we need exactly four digits
24 $ anchor meta chars syntax to anchor at the end of string



23. Match an E-mail Address

var sEmail = "somebody@adelphia.net"; var reObj = /^(?:\w+\.?)*\w+@(?:\w+\.)\w+$/; sEmail.match(reObj);   /^(?:\w+\.?)*\w+@(?:\w+\.)\w+$/; 000000000111111111122222222223333 123456789012345678901234567890123
The parts of the above regexp pattern
cols chars syntax Purpose
returns: somebody@adelphia.net
01&31 / / literal meta chars syntax for enclosing a regexp literal
02 ^ anchor meta chars syntax to anchor at beginning of string
03&12 ( ) grouping meta chars the parenthesis here indicate a group resides within
04-05 ?: meta chars specific to a group do not remember this group
subsequent references are not used or needed
06-07 \w a token for a predefined class syntax for designating word character
08 + repetition quantifier meta chars match one or more occurs of previous pattern
the previous pattern in this group is the word character token
09-10 \• escape a meta chars the forward slash escapes the period
the period can be a regexp meta character;
therefore, it must be escaped
11 ? repetition quantifier meta chars match zero or one occurs of previous pattern
the previous pattern is this group is the period
13 * repetition quantifier meta chars match zero or more occurs of previous pattern
the previous pattern is the group (?:\w+\•?)
The first group indicates what can occur before the mandatory at sign "@" in an E-mail address. This non-capturing group pattern (?:\w+\•?) indicates any number of word characters can be followed by zero or one period. The asterisk indicates that the former pattern (the group) can happen zero or more times. This group pattern (and following asterisk) will handle "John@" or "John.Doe@" or Johnney.come.lately@". Note that the at sign is shown here for illustration purposes but is actually handled in the next portion of the pattern.
14-15 \w a token for a predefined class syntax for designating word character
16 + repetition quantifier meta chars match one or more occurs of previous pattern
the previous pattern is the word character token
17 @ at sign is a non-word char part of the matching criteria
@ need not be escaped; not a meta char
The above pattern part indicates what must occur with the at sign. The \w+@ assures a word character always appears immediately before the at sign.
The next group, (?:\w+\•), is identical to the first group except the "?" quantifier is absent. So the pattern states that one or more word characters along with a period will follow the at sign.
18&26 ( ) grouping meta chars the parenthesis here indicate a group resides within
19-20 ?: meta chars specific to a group do not remember this group
subsequent references are not used or needed
21-22 \w a token for a predefined class syntax for designating word character
23 + repetition quantifier meta chars match one or more occurs of previous pattern
the previous pattern is this group is the word character token
24-25 \• escape a meta chars the forward slash escapes the period
the period can be a regexp meta character;
therefore, it must be escaped
The next pattern part, \w+, handles the required HTTP extension. It must be one or more word characters.
27-28 \w a token for a predefined class syntax for designating word character
29 + repetition quantifier meta chars match one or more occurs of previous pattern
the previous pattern is this group is the word character token
30 $ anchor meta chars syntax to anchor at the end of string



24. Match Day in dd/mm/ccyy Format

var sDay = "23"; var reObj = /0[1-9]|[12][0-9]|3[01]/; sDay.match(reObj);   /0[1-9]|[12][0-9]|3[01]/; 000000000111111111122222222223333 123456789012345678901234567890123
The parts of the above regexp pattern
cols chars syntax Purpose
returns: 23
01&24 / / literal meta chars syntax for enclosing a regexp literal
The regexp pattern for handling the dd portion of a date contains three alternatives.
  1. The first alternation, 0[1-9] will handle the scenario where the first digit is a zero. Here the second digit can be 1-9.
  2. The second alternation, [12][0-9] will handle the scenario where the first digit is 1 or 2. Here the second digit can be 0-9.
  3. The third alternation, 3[01] will handle the scenario where the first digit is 3. Here the second digit can be 0 or 1.
02 0
zero
literal char part of the matching criteria
this is the first digit of the dd
zero is one option for day, the others are 1,2 and 3
03&07 [ ] meta chars for character class embraces the characters that are valid for the second digit
04-06 1-9 the syntax for a range The hyphen indicates range class
the range is 1 thru 9
the allowable range of digits when the first digit is zero
08 | alternation meta chars syntax for designating an alternation
The second alternation starts here.
09&12 [ ] meta chars for character class embraces the characters that are valid for the first digit
10-11 12 literal chars the class of characters are: 1 and 2
the allowable range of digits for first digit in 2nd alternation
13&17 [ ] meta chars for character class embraces the characters that are valid for the second digit
14-16 0-9 literal chars in a range the class of characters are: 0 thru 9
the allowable range of digits for second digit in 2nd alternation
18 | alternation meta chars syntax for designating an alternation
The third alternation starts here.
19 3 literal char part of the matching criteria
this is the first digit of the dd
3 is one option for day, the others are 1,2 and 0
20&23 [ ] meta chars for character class embraces the characters that are valid for the second digit
21-22 01 literal chars the class of characters are: 1 and 0
the allowable range of digits for second digit in 3nd alternation when the first digit is 3



25. Match Month in dd/mm/ccyy Format

var sMonth = "07"; var reObj = /0[1-9]|1[0-2]/; sMonth.match(reObj);   /0[1-9]|1[0-2]/; 000000000111111111122222222223333 123456789012345678901234567890123
The parts of the above regexp pattern
cols chars syntax Purpose
returns: 07
01&15 / / literal meta chars syntax for enclosing a regexp literal
The regexp pattern for handling the mm portion of a date contains two alternatives.
  1. The first alternation, 0[1-9] will handle the scenario where the first digit is a zero. Here the second digit can be 1-9.
  2. The second alternation, 1[0-2] will handle the scenario where the first digit is 1. Here the second digit can be 0, 1 or 2. This applies to months: October, November and December.
02 0
zero
literal char part of the matching criteria
this is the first digit of the mm
03&07 [ ] meta chars for character class embraces the characters that are valid for the second digit
04-06 1-9 the syntax for a range The hyphen indicates range class
the range is 1 thru 9
the allowable range of digits when the first digit is zero
08 | alternation meta chars syntax for designating an alternation
The second alternation starts here.
09 1 literal char part of the matching criteria
this is the first digit of the mm
10&14 [ ] meta chars for character class embraces the characters that are valid for the second digit
11-13 0-2 the syntax for a range The hyphen indicates range class
the range is 0 thru 2
the allowable range of digits when the first digit is one



26. Match Year in dd/mm/ccyy Format

var sYear = "2007"; var reObj = /(?:19|20)\d{2}/g; sYear.match(reObj);   /(?:19|20)\d{2}/g; 000000000111111111122222222223333 123456789012345678901234567890123
The parts of the above regexp pattern
cols chars syntax Purpose
returns: 2007
01&16 / / literal meta chars syntax for enclosing a regexp literal
The regexp pattern for handling the ccyy portion of a date contains a group with two alternatives for the first two digits of cc. Here we are only allowing for the 20th and 21th centuries. The second two digits of yy can be any digit.
02&10 ( ) grouping meta chars the parenthesis here indicate a group resides within
03-04 ?: meta chars specific to a group do not remember this group
subsequent references are not used or needed
05-09 19|20 a group alternation the pipe indicates alternation
the group of 19 and 20 expressed as alternatives
11-12 \d a token for a predefined class syntax for designating the digits class
13-15 {2} the repetition criteria match the previous pattern exactly 2 times
the previous pattern is the predefined digits class
therefore, we need exactly two digits



27. Complete Pattern For European Format: dd/mm/ccyy

Combine the three patterns above, grouping them and adding the forward slash (escape them) that separate the date parts and we have this:

/(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20)\d{2}/g;


Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson