Regular Expressions
JavaScript 1.2 supports regular expressions, using the same syntax as
Perl 4. A regular expression is specified literally as a sequence of
characters within forward slashes (/), or as a JavaScript string
passed to the RegExp() constructor. The optional g
(global search)
and i
(case-insensitive search) modifiers may follow the second /
character, or may be passed to RegExp().
The following table summarizes regular expression syntax:
Character | Meaning |
---|---|
\n , \r , \t
|
Match literal newline, carriage return, tab |
\ , \/ , \* , \+ , \? , etc. |
Match a special character literally, ignoring or escaping its special meaning |
[ . . . ]
|
Match any one character between brackets |
[^ . . . ]
|
Match any one character not between brackets |
.
|
Match any character other than newline |
\w , \W
|
Match any word/nonword character |
\s , \S
|
Match any whitespace/nonwhitespace |
\d , \D
| Match any digit/nondigit |
^ , $
|
Require match at beginning/end of a string, or in multiline mode, beginning/end of a line |
\b , \B
|
Require match at a word boundary nonboundary |
?
|
Optional term; match zero or one time |
+
|
Match previous term one or more times |
*
|
Match term zero or more times |
{
n
}
|
Match previous term exactly n times |
{
n ,}
|
Match previous term n or more times |
{
n ,m
}
|
Match at least n but no more than m times |
a
|
b
| Match either a or b |
(
sub
)
|
Group subexpression sub into a single term, and remember the text that it matched |
n
|
Match exactly the same characters that were matched by subexpression ... |
Get Webmaster in a Nutshell, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.