RegExp Object
Regular expressions are objects that describe character patterns.
Regular expressions are used for pattern matching and search/replace on 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 using the constructor to create a regular expression object, normal character escape rules apply (prepend with backslash ). 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-insensitive and global matching:
| Modifier | Description |
|---|---|
| i | Perform case-insensitive matching. |
| g | Perform global matching (find all matches rather than stopping after the first match). |
| m | Perform multiline matching. |
Brackets
Brackets are used to find characters within a range:
| Expression | Description |
|---|---|
| Find any character between the brackets. | |
| [^abc] | Find any character not between the brackets. |
| Find any digit from 0 to 9. | |
| Find any character from lowercase a to lowercase z. | |
| Find any character from uppercase A to uppercase Z. | |
| Find any character from uppercase A to lowercase z. | |
| Find any character within the given set. | |
| [^adgk] | Find any character outside the given set. |
| (red|blue|green) | Find any of the specified options. |
Metacharacters
Metacharacters are characters with special meanings:
| Metacharacter | Description |
|---|---|
| . | Find a single character, except newline or line terminator. |
| w | Find word characters (digits, letters, underscore). |
| W | Find non-word characters. |
| d | Find digits. |
| D | Find non-digit characters. |
| s | Find whitespace characters. |
| S | Find non-whitespace characters. |
| b | Match a word boundary. |
| B | Match a non-word boundary. |
| Find the NULL character. | |
| n | Find a newline character. |
| f | Find a form feed character. |
| r | Find a carriage return character. |
| t | Find a tab character. |
| v | Find a vertical tab character. |
| xxx | Find the character specified by octal number xxx. |
| xdd | Find the character specified by hexadecimal number dd. |
| uxxxx | Find the Unicode character specified by 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 "a"s in "caaaaaaandy". |
| n* | Matches any string that contains zero or more occurrences of n. For example, /bo*/ matches "boooo" in "A ghost booooed", "b" in "A bird warbled", but not "A goat grunted". |
| n? | Matches any string that contains zero or one occurrence of n. For example, /e?le?/ matches "el" in "angel" and "le" in "angle". |
| n{X} | Matches a sequence of X occurrences of n. 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 consecutively. For example, /a{2,}/ does not match "a" in "candy", but matches all "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 times and at most Y times consecutively. For example, /a{1,3}/ does not match "cndy", matches "a" in "candy", 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 starts 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 | Retrieves the specified value in the string. Returns the found value and determines its position. |
| test | Retrieves the specified value in the string. Returns true or false. |
| toString | Returns the string representation of the regular expression. |
String Object Methods Supporting Regular Expressions
| Method | Description | FF | IE |
|---|---|---|---|
| search | Retrieves the value that matches the regular expression. | 1 | 4 |
| match | Finds one or more matches of the regular expression. | 1 | 4 |
| replace | Replaces substrings that match the 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 | Checks whether the "g" modifier is set. |
| ignoreCase | Checks whether the "i" modifier is set. |
| lastIndex | Specifies the starting position for the next match. |
| multiline | Checks whether the "m" modifier is set. |
| source | Returns the matching pattern of the regular expression. |
YouTip