simple reg·u·lar ex·pres·sion
| Attribute Character |
Description |
| g |
Global match. This looks for all matches of the pattern rather than stopping after the first match is found. |
| i |
Pattern is case-insensitive. For example, McNeill and mcneill are considered the same pattern of characters. |
| m |
Multi-line flag. Only available in IE 5.5+ and NN6+, this specifies that the special characters ^ and $ can match the beginning and the end of lines as well as the beginning and end of the string. |
Special Characters
| Attribute Character |
Characters It Matches |
Description |
| \d |
Any digit from 0 to 9 |
\d\d matches 65, but not aa or 6a |
| \D |
Any character that is not a digit |
\D\D\D matches abc, but not 123 or 8ef |
| \w |
Any word character; that is, A-Z, a-z, 0-9 and the underscore ( _ ) |
\w\w\w\w matches Ab_2, but not $%* or Ab_@ |
| \W |
Any non-word character |
\w matches @, but not a |
| \s |
Any whitespace character, including tab, newline, carriage return, formfeed and vertical tab |
\s matches tab |
| \S |
Any non-whitespace character |
\S matches A, but not the tab character |
| . |
Any single character other than the newline character ( \n ) |
. matches a or 4 or @ |
| [...] |
Any one of the characters between the brackets |
[abc] will match a or b or c but nothing else
[a-z] matches any character in the range a to z
|
| [^...] |
Any one character, but not one of those inside the brackets |
[^abc] will match any character except a or b or c
[^a-z] will match any character that is not in the range a to z
|
Repetition Characters
| Special Character |
Meaning |
Example |
| {n} |
Match n of the previous item. |
x{2} matches xx |
| {n, } |
Match n or more of previous item |
x{2,} matches xx, xxx, xxxx, xxxxx and so on |
| {n,m} |
Match at least n and at the most m of the previous item |
x{2,4} matches xx, xxx and xxxx |
| ? |
Match the previous item zero or one time |
x? matches x or nothing |
| + |
Match the previous item one or more times |
x+ matches x, xx, xxx, xxxx, xxxxx and so on |
| * |
Match the previous item zero or more times |
x* matches nothing, or x, xx, xxx, xxxx and so on |
Position Characters
| Position Character |
Description |
| ^ |
The pattern must be at the start of the string, or if it's a multi-line string, then at the beginning of a line. For multi-line text (a string that contains carriage returns), you need to set the multi-line flag when defining the regular expression string using /myregex/m. Note that this is only applicable to IE 5.5 and later and NN6 and later. |
| $ |
The pattern must be at the end of the string, or if it's a multi-line string, then at the end of a line. For multi-line text (a string that contains carriage returns), you need to set the multi-line flag when defining the regular expression string using /myregex/m. Note that this is only applicable to IE 5.5 and later and NN6 and later. |
| \b |
This matches a word boundary, which is essentially the point between a word character and a non-word character. |
| \B |
This matches a position that's not a word boundary. |
The above content was assembled from:
Beginning JavaScript
by Paul Wilton, Jeremy McPeak
Paperback, Third Edition, 1032 pages
Published May 21st 2007 by Wrox (first published 2000)
ISBN 0470051515 (ISBN13: 9780470051511)