The RegExp Object The regular expression object describes a pattern of characters. Syntax for creating a RegExp object: var txt=...
The RegExp Object
The regular expression object describes a pattern of characters.Syntax for creating a RegExp object:
var txt=new RegExp(pattern,attributes); or var txt=/pattern/attributes; |
- pattern specifies the pattern of the regular expression
- attributes specifies global ("g"), case-insensitive ("i"), and multiline matches ("m")
RegExp Object Properties
FF: Firefox, N: Netscape, IE: Internet ExplorerProperty | Description | FF | N | IE |
---|---|---|---|---|
global | Specifies if the "g" modifier is set | 1 | 4 | 4 |
ignoreCase | Specifies if the "i" modifier is set | 1 | 4 | 4 |
input | The string on which the pattern match is performed | 1 | 4 | 4 |
lastIndex | An integer specifying the index at which to start the next match | 1 | 4 | 4 |
lastMatch | The last matched characters | 1 | 4 | 4 |
lastParen | The last matched parenthesized substring | 1 | 4 | 4 |
leftContext | The substring in front of the characters most recently matched | 1 | 4 | 4 |
multiline | Specifies if the "m" modifier is set | 1 | 4 | 4 |
prototype | Allows you to add properties and methods to the object | 1 | 4 | 4 |
rightContext | The substring after the characters most recently matched | 1 | 4 | 4 |
source | The text used for pattern matching | 1 | 4 | 4 |
RegExp Object Methods
Method | Description | FF | N | IE |
---|---|---|---|---|
compile() | Change the regular expression | 1 | 4 | 4 |
exec() | Search a string for a specified value. Returns the found value and remembers the position | 1 | 4 | 4 |
test() | Search a string for a specified value. Returns true or false | 1 | 4 | 4 |
String Object Methods that supports Regular Expressions
Method | Description | FF | N | IE |
---|---|---|---|---|
search() | Search a string for a specified value. Returns the position of the value | 1 | 4 | 4 |
match() | Search a string for a specified value. Returns an array of the found value(s) | 1 | 4 | 4 |
replace() | Replace characters with other characters | 1 | 4 | 4 |
split() | Split a string into an array of strings | 1 | 4 | 4 |
RegExp Modifiers
Modifier | Description | FF | N | IE |
---|---|---|---|---|
i | Perform case-insensitive matching | 1 | 4 | 4 |
g | Perform a global match. Find all matches (do not stop after the first match) | 1 | 4 | 4 |
gi | Perform a global case-insensitive match. Find all matches (do not stop after the first match) | 1 | 4 | 4 |
m | Perform multiline matching | 1 | 4 | 4 |
RegExp Modifiers
Property | Description | FF | N | IE |
---|---|---|---|---|
Position Matching | ||||
^ | Get a match at the beginning of a string | 1 | 4 | 4 |
$ | Get a match at the end of a string | 1 | 4 | 4 |
\b | Word boundary. Get a match at the beginning or end of a word in the string | 1 | 4 | 4 |
\B | Non-word boundary. Get a match when it is not at the beginning or end of a word in the string | 1 | 4 | 4 |
?= | A positive look ahead. Get a match if a string is followed by a specific string | 1 | 4 | 4 |
?! | A negative look ahead. Get a match if a string is not followed by a specific string | 1 | 4 | 4 |
Literals | ||||
\0 | Find a NULL character | 1 | 4 | 4 |
\n | Find a new line character | 1 | 4 | 4 |
\f | Find a form feed character | 1 | 4 | 4 |
\r | Find a carriage return character | 1 | 4 | 4 |
\t | Find a tab character | 1 | 4 | 4 |
\v | Find a vertical tab character | 1 | 4 | 4 |
\xxx | Find the ASCII character expressed by the octal number xxx | 1 | 4 | 4 |
\xdd | Find the ASCII character expressed by the hex number dd | 1 | 4 | 4 |
\uxxxx | Find the ASCII character expressed by the UNICODE xxxx | 1 | 4 | 4 |
Character Classes | ||||
[xyz] | Find any character in the specified character set | 1 | 4 | 4 |
[^xyz] | Find any character not in the specified character set | 1 | 4 | 4 |
. (dot) | Find any character except newline or line terminator | 1 | 4 | 4 |
\w | Find any alphanumeric character including the underscore | 1 | 4 | 4 |
\W | Find any non-word character | 1 | 4 | 4 |
\d | Find any single digit | 1 | 4 | 4 |
\D | Find any non-digit | 1 | 4 | 4 |
\s | Find any single space character | 1 | 4 | 4 |
\S | Find any single non-space character | 1 | 4 | 4 |
Repetition | ||||
{x} | Finds the exact (x) number of the regular expression grouped together | 1 | 4 | 4 |
{x,} | Finds the exact (x) or more number of the regular expression grouped together | 1 | 4 | 4 |
{x,y} | Finds between x and y number of the regular expression grouped together | 1 | 4 | 4 |
? | Finds zero or one occurrence of the regular expression | 1 | 4 | 4 |
* | Finds zero or more occurrences of the regular expression | 1 | 4 | 4 |
+ | Finds one or more occurrences of the regular expression | 1 | 4 | 4 |
Grouping | ||||
( ) | Finds the group of characters inside the parentheses and stores the matched string | 1 | 4 | 4 |
(?: ) | Finds the group of characters inside the parentheses but does not store the matched string | 1 | 4 | 4 |
| | Combines clauses into one regular expression and then matches any of the individual clauses. Similar to "OR" statement | 1 | 4 | 4 |
Back references | ||||
( )\n | Back reference. Uses the stored matched string. i.e. from the ( ) modifier | 1 | 4 | 4 |