JavaScript RegExpObject


RegExp Object

Regular expressions are objects that describe character patterns.

Regular expressions are used for string pattern matching, searching, and replacing, and are powerful tools for performing pattern matching on strings.

Syntax

var patt=new RegExp(pattern,modifiers);

Or in a simpler way:

var patt=/pattern/modifiers;
  • pattern (the pattern) describes the pattern of the expression
  • modifiers are used to specify global matching, case-sensitive matching, and multi-line matching

Note:When using the constructor function to create a RegExp object, the regular character escaping rules are required (prefix with a backslash \). For example, the following are equivalent:

var re = new RegExp("\\w+");
var re = /\w+/;

For more about the RegExp object, please read ourJavaScript RegExp Object Tutorial。


Modifiers

Modifiers are used to perform case-sensitive and global matching:

Modifier Description
i Performs case-insensitive matching.
g Performs a global match (finds all matches rather than stopping after the first match is found).
m Performs multiline matching.

Brackets

Brackets are used to find characters within a certain range:

Expression Description
[abc] Finds any character between the brackets.
[^abc] Finds any character not between the brackets.
[0-9] Finds any digit from 0 to 9.
[a-z] Finds any character from lowercase a to lowercase z.
[A-Z] Finds any character from uppercase A to uppercase Z.
[A-z] Finds any character from uppercase A to lowercase z.
[adgk] Finds any character in the given set.
[^adgk] Finds any character outside the given set.
(red|blue|green) Finds any of the specified options.

Metacharacters

Metacharacters are characters that have special meanings:

Metacharacter Description
. Finds a single character, except for line breaks and line terminators.
\w Finds digits, letters, and underscores.
\W Finds non-word characters.
\d Finds a digit.
\D Finds a non-digit character.
\s Finds a whitespace character.
\S Finds a non-whitespace character.
\b Matches a word boundary.
\B Matches a non-word boundary.
\0 Finds a NULL character.
\n Finds a newline character.
\f Finds a form feed character.
\r Finds a carriage return character.
\t Finds a tab character.
\v Finds a vertical tab character.
\xxx Finds the character specified by the octal number xxx.
\xdd Finds the character specified by the hexadecimal number dd.
\uxxxx Finds the Unicode character specified by the hexadecimal number xxxx.

Quantifiers

Quantifier Description
n+

Matches any string that contains at least one n.

For example, /a+/ matches the "a" in "candy" and all the "a"'s in "caaaaaaandy".

n*

Matches any string that contains zero or more n's.

For example, /bo*/ matches "boooo" in "A ghost booooed", "b" in "A bird warbled", but does not match "A goat grunted".

n?

Matches any string that contains zero or one n.

For example, /e?le?/ matches "el" in "angel" and "le" in "angle".

n{X}

Matches strings containing a sequence of X n's.

For example, /a{2}/ does not match the "a" in "candy," but matches the two "a"'s in "caandy," and matches the first two "a"'s in "caaandy."

n{X,}

X is a positive integer. Matches when the preceding pattern n occurs consecutively at least X times.

For example, /a{2,}/ does not match the "a" in "candy", but matches all the "a"'s in "caandy" and "caaaaaaandy."

n{X,Y}

X and Y are positive integers. Matches when the preceding pattern n occurs consecutively at least X times and at most Y times.

For example, /a{1,3}/ does not match "cndy", matches the "a" in "candy,", the two "a"'s in "caandy,", and the first three "a"'s in "caaaaaaandy". Note that when matching "caaaaaaandy", even though the original string has more "a"'s, the match is "aaa".

n$ Matches any string that ends with n.
^n Matches any string that begins with n.
?=n Matches any string that is immediately followed by the specified string n.
?!n Matches any string that is not immediately followed by the specified string n.

RegExp Object Methods

Method Description
compile Deprecated in version 1.5.Compiles a regular expression.
exec Searches for a specified value in a string. Returns the found value and determines its position.
test Searches for a specified value in a string. Returns true or false.
toString Returns the string of the regular expression.

String Object Methods Supporting Regular Expressions

Method Description FF IE
search Searches for a value that matches a regular expression. 1 4
match Finds one or more matches of a regular expression. 1 4
replace Replaces substrings that match a regular expression. 1 4
split Splits a string into an array of strings. 1 4

RegExp Object Properties

Property Description
constructor Returns a function that is the prototype for creating RegExp objects.
global Determines whether the "g" modifier is set.
ignoreCase Determines whether the "i" modifier is set.
lastIndex Used to specify the starting position for the next match.
multiline Determines whether the "m" modifier is set.
source Returns the matching pattern of the regular expression.
Other Extensions