JavaScript supports regular expressions, which are often used for filtering and validating user input. Chapter 8 covers regular expressions in great detail. A few examples are shown in Table A-27 to remind you of their format.
Regular Expression |
Matches |
Does Not Match |
---|---|---|
/\Wten\W/ |
ten |
ten, tents |
/\wten\w/ |
aten1 |
ten, 1ten |
/\bten\b/ |
ten |
attention, tensile, often |
/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ |
128.22.45.1 |
abc.44.55.42, 128.22.45. |
/^(http|ftp|https):\/\/.*/ |
file:///etc/motd, https//www.w3c.org |
We summarize the important flags, repetition indicators, escape codes, and related object properties of regular expressions in Tables A-28 through A-33. See Chapter 8 for a detailed discussion.
Character |
Meaning |
---|---|
i |
Case-insensitive. |
g |
Global match. Find all matches in the string, rather than just the first. |
m |
Multiline matching. |
Character |
Meaning |
---|---|
* |
Match previous item zero or more times |
+ |
Match previous item one time or more |
? |
Match previous item zero or one times |
{m, n} |
Match previous item at minimum m times, but no more than n times |
{m, } |
Match previous item m or more times |
{m} |
Match previous item exactly m times |
Character |
Meaning |
---|---|
[chars] |
Any one character indicated either explicitly or as a range between the brackets |
[^chars] |
Any one character not between the brackets represented explicitly or as a range |
. |
Any character except newline |
\w |
Any word character. Same as [a-zA-Z0-9_] |
\W |
Any non-word character. Same as [^a-zA-Z0-9_] |
\s |
Any whitespace character. Same as [ \t\n\r\f\v] |
\S |
Any non-whitespace character. Same as [^ \t\n\r\f\v] |
\d |
Any digit. Same as [0-9] |
\D |
Any non-digit. Same as [^0-9] |
\b |
A word boundary. The empty “space” between a \w and \W |
\B |
A word non-boundary. The empty “space” between word characters |
[\b] |
A backspace character |
Code |
Matches |
---|---|
\f |
Form feed |
\n |
Newline |
\r |
Carriage return |
\t |
Tab |
\v |
Vertical tab |
\/ |
Foreslash (/) |
\\ |
Backslash (\) |
\. |
Period (.) |
\* |
Asterisk (*) |
\+ |
Plus sign (+) |
\? |
Question mark (?) |
\| |
Horizontal bar, aka Pipe (|) |
\( |
Left parenthesis (() |
\) |
Right parenthesis ()) |
\[ |
Left bracket ([) |
\] |
Right bracket (]) |
\{ |
Left curly brace ({) |
\} |
Right curly brace (}) |
\OOO |
ASCII character represented by octal value OOO |
\xHH |
ASCII character represented by hexadecimal value HH |
\uHHHH |
Unicode character represented by the hexadecimal value HHHH |
\cX |
Control character represented by ^X, for example, \cH represents Ctrl-h |
Feature |
Description |
---|---|
(?:expr) |
Non-capturing parentheses. Does not make the given parenthesized subexpression expr available for backreferencing. |
(?=expr) |
Positive lookahead. Forces the previous item to match only if it is followed by a string that matches expr. The text that matched expr is not included in the match of the previous item. |
(!expr) |
Negative lookahead. Forces the previous item to match only if it is not followed by a string matching expr. The text that did not match expr is not included in the match of the previous item. |
? |
Non-greedy matching. Forces the immediately preceding repetition quantifier to match the minimum number of characters required. |
Property |
Value |
---|---|
$1, $2, …, $9 |
Strings holding the text of the first nine parenthesized subexpressions of the most recent match. |
Holds the string index value of the first character in the most recent pattern match. This property is not part of the ECMA standard, though it is supported widely. Therefore it may be better to use the length of the RegExp pattern and the lastIndex property to calculate this value. |
|
input |
String containing the default string to match against the pattern. |
lastIndex |
Integer specifying the position in the string at which to start the next match. Same as the instance property, which should be used instead. |
lastMatch |
String containing the most recently matched text. |
lastParen |
String containing the text of the last parenthesized subexpression of the most recent match. |
leftContext |
String containing the text to the left of the most recent match. |
rightContext |
String containing the text to the right of the most recent match. |