HTML and CSS Reference
In-Depth Information
Figure H-5
Escape sequences
Escape Sequence
Represents
Regular Expression
Matches
\/
the / character
/\d\/\d/
5/9
\\
the \ character
/\d\\\d/
5\9
\.
the . character
/\d\.\d\d/
5.99
\*
the * character
/[a-z]{4}\*/
info*
\+
the + character
/\d\+\d/
5+9
\?
the ? character
/[a-z]{5}\?/
user?
\|
/a\|b/
the | character
a|b
\( \)
the ( and ) characters
/\(\d{3}\)/
(414)
\{ \}
the { and } characters
/\{[a-z]{4}\}/
{pass}
\^
the ^ character
/\d+\^\d/
321^2
\$
the $ character
/\$\d{2}\.\d{2}/ $59.95
\n
a new line
/\n/
an occurrence of a
new line
\r
a carriage return
/\r/
an occurrence of a
carriage return
\t
a tab
/\t/
an occurrence of a tab
Note that JavaScript treats a regular expression pattern created with the new object con-
structor as a text string. Even though they are not regular expressions, such text strings
can also contain escape sequences, which can be used to insert non-textual characters
such as tabs or returns. To avoid conflict between escape sequences designed for text
strings and escape sequences designed for regular expressions, you must insert an addi-
tional escape character \ for each regular expression escape sequence. Thus the regular
expression literal
/\b\w*\b\s\d{3}/
would appear in the object constructor as
new RegExp(“\\b\\w*\\b\\s\\d{3}”)
In most cases, you'll use the regular expression literal form in code. The new RegExp()
operator is most often used when a script needs to retrieve a regular expression text
string from another source, such as a data entry field.
Specifying Alternate Patterns and Grouping
In some regular expressions, you may want to define two possible patterns for the same
text string. You can do this by joining different patterns using the | character. The general
form is
pattern1 | pattern2
where pattern1 and pattern2 are two distinct patterns. For example, the expression
/^\d{5}$|^$/
matches a text string that either contains only five digits or is empty. The regular expres-
sion pattern
/Mr\.|Mrs\.|Miss/
matches Mr. , Mrs. , or Miss .
 
Search WWH ::




Custom Search