JS RegExp Object
RegExp object
A regular expression is an object that describes a character pattern.
Regular expressions are used to match and retrieve substitutions for string patterns and are powerful tools for performing pattern matching on strings.
Grammar
var patt=new RegExp(pattern,modifiers);
Or a simpler way:var patt=/pattern/modifiers;
pattern describes the pattern of an expression
modifiers are used to specify global matching, case-sensitive matching, and multiline matching
Note: when using constructors to create regular objects, the general character escape rules (preceded by a backslash \) are required. for example, the following is equivalent:
var re = new RegExp("\\w+");
var re = /\w+/;
For more information about RegExp objects, read our JavaScript RegExp Objects 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 instead of stopping when the first match is found). |
m | Perform multi-row matching. |
Square bracket
Square brackets are used to find characters in a range:
expression | description |
---|---|
[abc] | Finds any character between square brackets. |
[^abc] | Look for any character that is not between square brackets. |
[0-9] | Find any number from 0 to 9. |
[a-z] | Finds any character from lowercase a to lowercase z. |
[A-Z] | Find any character from uppercase a to uppercase z. |
[A-z] | Finds any character 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) | Find any of the specified options. |
Metacharacters
Metacharacter is a character with a special meaning:
Metacharacter | Description |
---|---|
. | Find individual characters, except for line breaks and line end characters. |
\w | Find numbers, letters, and underscores. |
\W | Find non-word characters. |
\d | Find the number. |
\D | Find non-numeric characters. |
\s | Find whitespace characters. |
\S | Find non-whitespace characters. |
\b | Match word boundaries. |
\B | Match non-word boundaries. |
\0 | Look for null characters. |
\n | Look for line breaks. |
\f | Look for page feeds. |
\r | Look for carriage returns. |
\t | Look up tabs. |
\v | Find vertical tabs. |
\xxx | Look for characters specified in the octal number xxx. |
\xdd | Look for characters specified in the hexadecimal number dd. |
\uxxxx | Look for Unicode characters specified in the hexadecimal number xxxx. |
Measure words
Measure word | Description |
---|---|
n+ | Matches any string that contains at least one n. For example, /a+/ matches "a" in "candy", all "a" in "caaaaaaandy". |
n* | Matches any string containing zero or more n. For example, /bo*/ matches "boooo" in "A ghost booooed", "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 a string that contains a sequence of X n's. For example, /a{2}/ does not match "a" in "candy,", but matches two "a" in "caandy," and matches the first two "a" in "caaandy.". |
n{X,} | X is a positive integer. The preceding pattern n matches at least X consecutive occurrences. For example, /a{2,}/ does not match "a" in "candy", but matches all "a" in "caandy" and "caaaaaaandy.". |
n{X,Y} | X and Y are positive integers. The preceding pattern n appears at least X times in a row and matches at most Y times. For example, /a{1,3}/ does not match "cndy", matches the one "a" in "candy, matches the tow "aa" in "caandy,", and matches the first three "a" in "caaaaaaandy". Note that when "caaaaaaandy" is matched, the match is "aaa" even though the original string has more "a". |
n$ | Matches any string that ends in n. |
^n | Matches any string that begins 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 methods
Method | Description |
---|---|
compile | Deprecated in version 1.5. compile the regular expression. |
exec | Retrieves the value specified in the string. returns the found value and determines its location. |
test | Retrieves the value specified in the string. returns true or false. |
toString | Returns a string of regular expressions. |
A method of a String object that supports regular expressions
Method | Description | FF | IE |
---|---|---|---|
search | Retrieves a value that matches a regular expression. | 1 | 4 |
match | A match was found for one or more regular expressions. | 1 | 4 |
replace | Replaces the substring that matches the regular expression. | 1 | 4 |
split | Splits a string into string arrays. | 1 | 4 |
RegExp object properties
property | Description |
---|---|
constructor | Returns a function that is a prototype for creating a RegExp object. |
global | Determines whether the "g" modifier is set |
ignoreCase | Determines whether the "i" modifier is set |
lastIndex | Used to specify the starting position for the next match |
multiline | Determines whether the "m" modifier is set |
source | Returns the matching pattern of a regular expression |