|
JavaScript RegExp Object Reference
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, IE: Internet Explorer
| Property |
Description |
FF |
IE |
| global |
Specifies if the "g" modifier is set |
1 |
4 |
| ignoreCase |
Specifies if the "i" modifier is set |
1 |
4 |
| input |
The string on which the pattern match is performed |
1 |
4 |
| lastIndex |
An integer specifying the index at which to start the next
match |
1 |
4 |
| lastMatch |
The last matched characters |
1 |
4 |
| lastParen |
The last matched parenthesized substring |
1 |
4 |
| leftContext |
The substring in front of the characters most recently
matched |
1 |
4 |
| multiline |
Specifies if the "m" modifier is set |
1 |
4 |
| prototype |
Allows you to add properties and methods to the object |
1 |
4 |
| rightContext |
The substring after the characters most recently matched |
1 |
4 |
| source |
The text used for pattern matching |
1 |
4 |
RegExp Object Methods
| Method |
Description |
FF |
IE |
| compile() |
Change the regular expression |
1 |
4 |
| exec() |
Search a string for a specified value. Returns the found
value and remembers the position |
1 |
4 |
| test() |
Search a string for a specified value. Returns true or
false |
1 |
4 |
String Object Methods that supports Regular Expressions
| Method |
Description |
FF |
IE |
| search() |
Search a string for a specified value. Returns the
position of the value |
1 |
4 |
| match() |
Search a string for a specified value. Returns an array of
the found
value(s) |
1 |
4 |
| replace() |
Replace characters with other characters |
1 |
4 |
| split() |
Split a string into an array of strings |
1 |
4 |
RegExp Modifiers
| Modifier |
Description |
FF |
IE |
| i |
Perform case-insensitive matching |
1 |
4 |
| g |
Perform a global match. Find all matches (do not stop after
the first match) |
1 |
4 |
| gi |
Perform a global case-insensitive match. Find all matches
(do not stop after the first match) |
1 |
4 |
| m |
Perform multiline matching |
1 |
4 |
RegExp Modifiers - Position Matching
| Modifier |
Description |
FF |
IE |
| ^ |
Get a match at the beginning of a string |
1 |
4 |
| $ |
Get a match at the end of a string |
1 |
4 |
| \b |
Word boundary. Get a match at the beginning or end of a
word in the string |
1 |
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 |
| ?= |
A positive look ahead. Get a match if a string is followed
by a specific string |
1 |
4 |
| ?! |
A negative look ahead. Get a match if a string is not
followed by a specific string |
1 |
4 |
RegExp Modifiers - Literals
| Modifier |
Description |
FF |
IE |
| \0 |
Find a NULL character |
1 |
4 |
| \n |
Find a new line character |
1 |
4 |
| \f |
Find a form feed character |
1 |
4 |
| \r |
Find a carriage return character |
1 |
4 |
| \t |
Find a tab character |
1 |
4 |
| \v |
Find a vertical tab character |
1 |
4 |
| \xxx |
Find the ASCII character expressed by the octal number
xxx |
1 |
4 |
| \xdd |
Find the ASCII character expressed by the hex number dd |
1 |
4 |
| \uxxxx |
Find the ASCII character expressed by the UNICODE xxxx |
1 |
4 |
RegExp Modifiers - Character Classes
| Modifier |
Description |
FF |
IE |
| [xyz] |
Find any character in the specified character set |
1 |
4 |
| [^xyz] |
Find any character not in the specified character set |
1 |
4 |
| . (dot) |
Find any character except newline or line terminator |
1 |
4 |
| \w |
Find any alphanumeric character including the underscore |
1 |
4 |
| \W |
Find any non-word character |
1 |
4 |
| \d |
Find any single digit |
1 |
4 |
| \D |
Find any non-digit |
1 |
4 |
| \s |
Find any single space character |
1 |
4 |
| \S |
Find any single non-space character |
1 |
4 |
RegExp Modifiers - Repetition
| Modifier |
Description |
FF |
IE |
| {x} |
Finds the exact (x) number of the regular expression
grouped together |
1 |
4 |
| {x,} |
Finds the exact (x) or more number of the regular expression
grouped together |
1 |
4 |
| {x,y} |
Finds between x and y number of the regular expression
grouped together |
1 |
4 |
| ? |
Finds zero or one occurrence of the regular expression |
1 |
4 |
| * |
Finds zero or more occurrences of the regular expression |
1 |
4 |
| + |
Finds one or more occurrences of the regular expression |
1 |
4 |
RegExp Modifiers - Grouping
| Modifier |
Description |
FF |
IE |
| ( ) |
Finds the group of characters inside the parentheses and
stores the matched string |
1 |
4 |
| (?: ) |
Finds the group of characters inside the parentheses but
does not store the matched string |
1 |
4 |
| | |
Combines clauses into one regular expression and then
matches any of the individual clauses. Similar to "OR" statement |
1 |
4 |
RegExp Modifiers - Back Reference
| Modifier |
Description |
FF |
IE |
| ( )\n |
Back reference. Uses the stored matched string. i.e. from
the ( ) modifier |
1 |
4 |
 |
W3Schools' Online Certification Program
The perfect solution for professionals who need to balance work, family, and career building.
More than 3500 certificates already issued!
|
The HTML Certificate documents your knowledge of HTML, XHTML, and CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
|