JavaScript RegExp Object
RegExp object
A regular expression is a character description of the object model.
Regular expressions are used to retrieve the string pattern matching and replacement, it is a powerful tool for the implementation of the string pattern matching.
grammar
var patt=new RegExp(pattern,modifiers);
或者更简单的方式:
var patt=/pattern/modifiers;
或者更简单的方式:
var patt=/pattern/modifiers;
- pattern (pattern) describes the expression pattern
- modifiers (modifiers) used to specify the global matching, and multi-line distinction match with matching case
Note: When using a constructor to create a regular objects, you need regular character escape rules (preceded by a backslash \). For example, the following are equivalent:
var re = new RegExp("\\w+"); var re = /\w+/;
Read more about RegExp object in our JavaScript RegExp objects tutorial .
Modifiers
Modifier is used to perform a case-sensitive and global match:
Modifiers | description |
---|---|
i | Perform case-insensitive match. |
g | Perform a global match (find all matches rather than in the first match after stopping). |
m | Executive Multi-line matches. |
Square brackets
Square brackets are used to find a range of characters:
expression | description |
---|---|
[abc] | Find any character between the brackets. |
[^ abc] | Find any character not between the brackets. |
[0-9] | Look for any number from 0-9. |
[Az] | Look for any small to write a lowercase z character. |
[AZ] | Find any character from uppercase A to Z, uppercase. |
[Az] | Find any character from uppercase A to lowercase z's. |
[Adgk] | Searches for any character within a given set. |
[^ Adgk] | Searches for any character outside the given set. |
(Red | blue | green) | Find any options specified. |
Metacharacters
Metacharacter (Metacharacter) character has a special meaning:
Metacharacters | description |
---|---|
. | Find a single character, except newline and line endings. |
\ w | Find a word character. |
\ W | Find a non-word character. |
\ d | Find figures. |
\ D | Finding non-numeric characters. |
\ s | Find a blank character. |
\ S | Finding non-whitespace characters. |
\ b | Match word boundary. |
\ B | Match non-word boundary. |
\ 0 | Find NUL characters. |
\ n | Find newline. |
\ F | Find page breaks. |
\ R | Find a carriage return. |
\ T | Find tabs. |
\ V | Find a vertical tab. |
\ xxx | Look for the octal number xxx predetermined characters. |
\ xdd | Find a hexadecimal number dd predetermined characters. |
\ uxxxx | Find a hexadecimal number xxxx specified Unicode character. |
quantifier
quantifier | description |
---|---|
n + | Any string that contains at least one n match. |
n * | Any string that contains zero or more n match. |
n? | Any material that contains zero or one n string matching. |
n {X} | A string of X contains n sequence matches. |
n {X, Y} | A string of X or Y n contains a sequence matching. |
n {X,} | At least X n sequence contains string matching. |
n $ | N matches any string ending. |
^ n | N match any string beginning with a. |
? = n | Immediately thereafter any specified string n string matching. |
?! n | Any subsequent not immediately specified string n string matching. |
RegExp object methods
method | description | FF | IE |
---|---|---|---|
compile | Compiling a regular expression. | 1 | 4 |
exec | The value specified search string. The return value of the find, and to determine its position. | 1 | 4 |
test | The value specified search string. Returns true or false. | 1 | 4 |
It supports regular expressions String object methods
method | description | FF | IE |
---|---|---|---|
search | Search and regular expression matching values. | 1 | 4 |
match | Find one or more regular expression matching. | 1 | 4 |
replace | Replace with regular expression matching substring. | 1 | 4 |
split | The string is divided into an array of strings. | 1 | 4 |