JavaScript RegExp Object
RegExp object
A regular expression is an object that describes a character pattern.
Regular expressions are used for pattern matching, searching, and replacing in strings; they are a powerful tool for performing pattern matching on strings.
Syntax
var patt=new RegExp(pattern,modifiers);
Or a simpler way:
var patt=/pattern/modifiers;
- pattern describes the pattern of the expression
- modifiers are used to specify global matching, case-insensitive matching, and multiline matching
Note:When creating a regular expression object with the constructor, the usual character escaping rules apply (add a backslash \ in front). For example, the following are equivalent:
var re = new RegExp("\w+"); var re = /\w+/;
For more about the RegExp object, please read our JavaScript RegExp object tutorial.
Modifiers
Modifiers are used to perform case-sensitive and global matching:
| Modifiers | Description |
|---|---|
| i | Performs a case-insensitive match. |
| g | Performs a global match (finds all matches rather than stopping after the first match). |
| m | Performs a multiline match. |
Brackets
Brackets are used to find a character 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 not in the given set. |
| (red|blue|green) | Finds any of the specified alternatives. |
Metacharacter
A metacharacter is a character with a special meaning:
| Metacharacter | Description |
|---|---|
| . | Finds a single character, except for a newline or line terminator. |
| \w | Finds a digit, letter, or underscore. |
| \W | Finds a non-word character. |
| \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. |
Quantifier
| Quantifier | Description |
|---|---|
| n+ |
Matches any string containing at least one n. For example, /a+/ matches the "a" in "candy" and all "a"s in "caaaaaaandy". |
| n* |
Matches any string containing zero or more n. For example, /bo*/ matches "boooo" in "A ghost booooed" and "b" in "A bird warbled", but does not match "A goat grunted". |
| n? |
Matches any string containing zero or one n. For example, /e?le?/ matches "el" in "angel" and "le" in "angle". |
| n{X} |
Matches any string 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 the first two "a"s in "caaandy.". |
| n{X,} |
X is a positive integer. Matches when the preceding pattern n occurs at least X times in a row. 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 at least X and at most Y times in a row. 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 still "aaa". |
| n$ | Matches any string ending with n. |
| ^n | Matches any string beginning with n. |
| ?=n | Matches any string that is followed by the specified string n. |
| ?!n | Matches any string that is not followed by the specified string n. |
RegExp object methods
| Method | Description |
|---|---|
| compile | Deprecated in version 1.5. Compiles a regular expression. |
| exec | Searches a string for a specified value. Returns the value found and determines its position. |
| test | Searches a string for a specified value. Returns true or false. |
| toString | Returns the string of the regular expression. |
String object methods that support 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 | Specifies the starting position for the next match. |
| multiline | Determines whether the "m" modifier is set. |
| source | Returns the matching pattern of the regular expression. |