Press "Enter" to skip to content

Regular Expressions and JavaScript

About Regular Expressions

Regular expressions are an amazingly powerful way to validate and format text strings. Using regular expressions, you can write a line or two of JavaScript code that can accomplish tasks that otherwise would have taken several dozen lines.

A regular expression (often abbreviated as RegExp or called by its synonym grep) is a pattern, written using special symbols, which describes one or more text strings. You use regular expressions to match patterns of text, so that your script can easily recognize and manipulate text. Like an arithmetic expression, you create a regular expression by using operators, in this case operators that work on text, rather than numbers. There are many regular expression operators. By learning and using these operators, you’ll be able to save yourself a huge amount of effort whenever you need to detect and manipulate text strings.

Table 1: Regular Expression Special Characters
Character Matches
\ Toggles between literal and special characters; for example, “\w” means the special value of “\w” (see below) instead of the literal “w”, but “\$” means to ignore the special value of “$” (see below) and use the “$” character instead
^ Beginning of a string
$ End of a string/td>
* Zero or more times
+ One or more times
? Zero or one time
. Any character except newline
\b Word boundary
\B Non-word boundary
\d Any digit 0 through 9 (same as [0-9])
\D Any non-digit
\f Form feed
\n New line
\r Carriage return
\s Any single white space character (same as [ \f\n\r\t\v])
\S Any single non-white space character
\t Tab
\v Vertical tab
\w Any letter, number, or the underscore (the same as [a-zA-Z0-9_])
\W Any character other than a letter, number, or underscore
\xnn The ASCII character defined by the hexadecimal number nn
\onn The ASCII character defined by the octal number nn
\xC The control character X
[abcde] A character set that matches any one of the enclosed characters
[^abced] A complemented or negated character set; one that does not match any of the enclosed characters
[a-e] A character set that matches any one in the range of enclosed characters
[\b] The literal backspace character (different form \b)
{n} Exactly n occurrences of the previous character
{n,} At least n occurrences of the previous character
{n,m} Between n and m occurrences of the previous character
() A grouping, which is also stored for later use
x|y Either x or y
Table 2: Regular Expression Modifiers
Modifier Meaning
g Search for all possible matches (globally), not just the first
i Search without case-sensitivity

About the RegExp Object

JavaScript has a built-in RegExp object that’s automatically set (and reset) every time a script executes a regular expression method (given in Table 4 and 5). The properties of this object are shown in Table 3 and its methods in Table 4. The RegExp object isn’t a variable that contains the result of the regular expression operation, but rather it contains the pattern described by the regular expression, in a form that can be used in your scripts via the RegExpobject’s properties and methods.

Table 3: Properties of the RegExp Object
Properties Meaning
$1 (through $9) Parenthesizxed substring
$_ Same as input
$* Same as multiline
$& Same as lastMatch
$+ Same as lastParen
$` Same as leftContext
$’ Same as rightContext
constructor Specifies that function that creates an object’s prototype
global Search globally (g modifier in use)
ignoreCase Search case-insensitive (i modifier in use)
input The string to search if no string is passed
lastIndex The index at which to start the next match
lastMatch The last matched characters
lastParen The last parenthesized substring match
leftContext The substring to the left of the most recent match
multiline Whether strings are searched across multiple lines
prototype Allows the addition of properties to all objects
rightContext The substring to the right of the most recent match
source The regular expression
Table 4: Methods of the RegExp Object
compile(pattern, [, “g” | “i” | “gi”]) Compiles a regular expression
exec(string) Executes a search for a match
test(string) Tests for a match
toSource() Returns a literal representing the object
soString() Returns a string representing the specified object
valueOf() Returns the primitive value of the specified object
Table 5: String Methods
Methods Meaning
match(re) Finds a match for a regular expression pattern (re) within a string
replace(re,replaceStr) Using the regular expression (re), performs the desired replacement
search(re) Searches for a match to the regular expression (re)
split(re) Splits a string based on a regular expression (re)

Reference: Chapter 7 of JavaScript Visual QuickStart Guide (8th Edition)

Be First to Comment


电子邮件地址不会被公开。 必填项已用*标注