JavaScript RegExp object
RegExp object
Regular expressions are objects that describe character patterns.
Regular expressions are a powerful tool for string pattern matching and retrieval substitution and perform pattern matching of strings.
Grammar
Or simpler way:
var patt=/pattern/modifiers;
- Pattern describes the pattern of an expression
- modifiers are used to specify global matches, case-sensitive matches, and multi-line matches
Note: When you create a regular object using a constructor, you need a regular character escape rule (add a backslash before it). For example, here's the equivalent:
var re = new RegExp("\\w+"); var re = /\w+/;
For more information on RegExp objects, read our JavaScript RegExp Objects tutorial.
Modifier
Modifiers are used to perform case-sensitive and global matches:
Modifier | Describe |
---|---|
Ⅰ | Performs a case-insensitive match. |
G | Perform a global match (find all matches instead of stopping when the first match is found). |
M | Perform a multi-line match. |
Square brackets
Square brackets are used to find characters in a range:
The expression | Describe |
---|---|
[abc] | Find any character between square brackets. |
[^abc] | Find any characters that are not between square brackets. |
[0-9] | Look for any numbers from 0 to 9. |
[a-z] | Look for any characters from small case a to smaller z. |
[A-Z] | Find any characters from capital A to capital Z. |
[A-z] | Look for any characters from capital A to smaller z. |
[adgk] | Find any characters within a given collection. |
[^adgk] | Find any characters outside a given collection. |
(red|blue|green) | Look for any of the specified options. |
Meta-characters
Metacharacter is a character with a special meaning:
Meta-characters | Describe |
---|---|
. | Look for individual characters, except line breaks and line endings. |
\w | Find word characters. |
\W | Find non-word characters. |
\d | Find the number. |
\D | Find non-numeric characters. |
\s | Look for blank characters. |
\S | Find non-blank characters. |
\b | Match word boundaries. |
\B | Match non-word boundaries. |
\0 | Find NUL characters. |
\n | Look for line breaks. |
\f | Look for page breaks. |
\r | Look for carriage returns. |
\t | Look for tabs. |
\v | Look for vertical tabs. |
\xxx | Find the characters specified in octal xxx. |
\xdd | Look for characters specified in the hete sixteenth-order number dd. |
\uxxxx | Look for Unicode characters specified in hex xxxx. |
Quantifiers
Quantifiers | Describe |
---|---|
n+ | Matches any string that contains at least one n. |
N | Matches any string that contains zero or more n. |
N? | Matches any string that contains zero or an n. |
n{X} | Matches a string that contains a sequence of X n. |
n{X,Y} | Matches a string that contains sequences of X to Y n. |
n{X,} | Matches a string that contains at least X n of the sequence. |
n$ | Matches any string that ends with n. |
^n | Matches any string that starts with n. |
?=n | Matches any string immediately following the specified string n. |
?! N | Matches any string that is not immediately followed by the specified string n. |
RegExp object method
Method | Describe | Ff | Ie |
---|---|---|---|
compile | Compile regular expressions. | 1 | 4 |
Exec | Retrieves the value specified in the string. Returns the value found and determines its location. | 1 | 4 |
test | Retrieves the value specified in the string. Return true or false. | 1 | 4 |
A method that supports a String object for regular expressions
Method | Describe | Ff | Ie |
---|---|---|---|
search | Retrieves the value that matches the regular expression. | 1 | 4 |
match | A match was found for one or more regular expressions. | 1 | 4 |
replace | Replaces a substruce that matches a regular expression. | 1 | 4 |
split | Split the string into an array of strings. | 1 | 4 |