The regular expression describes the pattern object of the character
Grammar:
var patt=New RegExp (pattern,modifiers); or the simpler way var patt=/pattern/modifiers;
- The pattern describes an expression model.
- The modifier (modifiers) describes whether the retrieval is global, case sensitive, and so on.
Note: When you use constructors to create regular objects, you need a regular character escaping rule (preceded by a backslash \). For example, the following are equivalent:
var New REGEXP ("\\w+"); var re =/\w+/;
- Regular expression modifiers
- The i -modifier is used to perform a case-insensitive match.
- The g -modifier is used to perform a full-text search (instead of finding the first one to stop looking, but to find all matches).
- Method:
- modifier: modifier is used to perform case-sensitive and global matching
modifier |
description |
I |
performs a match that is case insensitive. |
G |
performs a global match (finds all matches rather than stops after the first match is found). |
m |
performs multi-line matching. |
- Square brackets: square brackets are used to find characters in a range:
An expression |
Description |
[ABC] |
Look for any characters between square brackets. |
[^ABC] |
Look for any characters that are not in square brackets. |
[0-9] |
Look for any number from 0 to 9. |
[A-z] |
Finds any characters from a to lowercase z from a small write. |
[A-z] |
Look for any characters from uppercase A to uppercase Z. |
[A-z] |
Look for any characters from uppercase A to lowercase z. |
[ADGK] |
Finds any character within a given collection. |
[^ADGK] |
Finds any character outside the given collection. |
(Red|blue|green) |
Finds any of the specified options. |
- metacharacters: meta-character (Metacharacter) is a character with a special meaning:
metacharacters |
description |
. | The
finds a single character, in addition to line breaks and line terminators. |
\w |
to find word characters. |
\w |
to find non-word characters. |
\d |
to find numbers. |
\d |
to find non-numeric characters. |
\s |
to find white space characters. |
\s |
to find non-whitespace characters. |
\b |
matches the word boundary. |
\b |
matches non-word boundaries. |
\ |
find NUL characters. |
\ n |
find line breaks. |
\f |
Find a page break. |
\ r |
find a carriage return. |
\ t |
find tabs. |
\v |
to find vertical tabs. |
\xxx |
to find characters that are specified in octal number XXX. |
\xdd |
to find the characters specified in hexadecimal digits dd. |
\uxxxx |
finds Unicode characters that are specified in hexadecimal number xxxx. |
- quantifier
quantifier |
description |
n+ | td> matches any string that contains at least one n.
n |
matches any string containing 0 or more n. |
n? | The
matches any string that contains 0 or one n. |
n{x} |
matches a string containing a sequence of X N. |
n{x,y} |
matches a string containing a sequence of X or Y N. |
n{x,} |
matches a string containing a sequence of at least X N. |
n$ |
matches any string that ends with N. |
^n |
matches any string that begins with N. |
? =n |
matches any string immediately followed by the specified string n. |
?! n |
matches any string that is not followed by the specified string n. |
- RegExp Object Method
method |
Description |
FF |
ie |
compile |
compiles the regular expression. |
1 |
4 |
exec |
Retrieves the value specified in the string. Returns the found value and determines its location. |
1 |
4 |
Test |
Retrieves the value specified in the string. Returns TRUE or FALSE. |
1 |
4 |
About compile method:
- A String object method that supports regular expressions
Method |
Description |
FF |
IE |
Search |
Retrieves a value that matches a regular expression. |
1 |
4 |
Match |
Finds a match for one or more regular expressions. |
1 |
4 |
Replace |
Replace the substring that matches the regular expression. |
1 |
4 |
Split |
Splits a string into an array of strings. |
1 |
4 |
Reference:
JavaScript REGEXP Objects
JavaScript RegExp Object reference manual
Compile method (regular expression) (JavaScript)
"JS" about regular expressions