JavaScript RegExp Object
RegExp Object
A regular expression is an object that describes a pattern of characters.
Regular expressions are used to perform powerful pattern-matching and "search-and-replace" functions on text.
Syntax
var txt=new RegExp(pattern,modifiers);
or more simply:
var txt=/pattern/modifiers;
|
- pattern specifies the pattern of an expression
- modifiers specify if a search should be global, case-sensitive, etc.
Modifiers
Modifiers are used to perform case-insensitive and global searches:
| Modifier |
Description |
F |
IE |
| i |
Perform case-insensitive matching |
1 |
4 |
| g |
Perform a global match (find all matches rather than stopping after the first match) |
1 |
4 |
| m |
Perform multiline matching |
1 |
4 |
Brackets
Brackets are used to find a range of characters:
| Expression |
Description |
F |
IE |
| [abc] |
Find any character between the brackets |
1 |
4 |
| [^abc] |
Find any character not between the brackets |
1 |
4 |
| [0-9] |
Find any digit from 0 to 9 |
1 |
4 |
| [a-z] |
Find any character from lowercase a to lowercase z |
1 |
4 |
| [A-Z] |
Find any character from uppercase A to uppercase Z |
1 |
4 |
| [a-Z] |
Find any character from lowercase a to uppercase Z |
1 |
4 |
| [adgk] |
Find any character in the given set |
1 |
4 |
| [^adgk] |
Find any character outside the given set |
1 |
4 |
| [red|blue|green] |
Find any of the alternatives specified |
1 |
4 |
Metacharacters
Metacharacters are characters with a special meaning:
| Metacharacter |
Description |
F |
IE |
| . |
Find a single character, except newline or line terminator |
1 |
4 |
| \w |
Find a word character |
1 |
4 |
| \W |
Find a non-word character |
1 |
4 |
| \d |
Find a digit |
1 |
4 |
| \D |
Find a non-digit character |
1 |
4 |
| \s |
Find a whitespace character |
1 |
4 |
| \S |
Find a non-whitespace character |
1 |
4 |
| \b |
Find a match at the beginning/end of a word |
1 |
4 |
| \B |
Find a match not at the beginning/end of a word |
1 |
4 |
| \0 |
Find a NUL 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 character specified by an octal number xxx |
1 |
4 |
| \xdd |
Find the character specified by a hexadecimal number dd |
1 |
4 |
| \uxxxx |
Find the Unicode character specified by a hexadecimal number xxxx |
1 |
4 |
Quantifiers
| Quantifier |
Description |
F |
IE |
| n+ |
Matches any string that contains at least one n |
1 |
4 |
| n* |
Matches any string that contains zero or more occurrences of n |
1 |
4 |
| n? |
Matches any string that contains zero or one occurrences of n |
1 |
4 |
| n{X} |
Matches any string that contains a sequence of X n's |
1 |
4 |
| n{X,Y} |
Matches any string that contains a sequence of X or Y n's |
1 |
4 |
| n{X,} |
Matches any string that contains a sequence of at least X
n's |
1 |
4 |
| n$ |
Matches any string with n at the end of it |
1 |
4 |
| ^n |
Matches any string with n at the beginning of it |
1 |
4 |
| ?=n |
Matches any string that is followed
by a specific string n |
1 |
4 |
| ?!n |
Matches any string that is not followed
by a specific string n |
1 |
4 |
RegExp Object Properties
F: Firefox, IE: Internet Explorer
| Property |
Description |
F |
IE |
| global |
Specifies if the "g" modifier is set |
1 |
4 |
| ignoreCase |
Specifies if the "i" modifier is set |
1 |
4 |
| lastIndex |
The index at which to start the next match |
1 |
4 |
| multiline |
Specifies if the "m" modifier is set |
1 |
4 |
| source |
The text of the RegExp pattern |
1 |
4 |
RegExp Object Methods
| Method |
Description |
F |
IE |
| compile() |
Compiles a regular expression |
1 |
4 |
| exec() |
Tests for a match in a string. Returns a result array |
1 |
4 |
| test() |
Tests for a match in a string. Returns true or
false |
1 |
4 |
 |
W3Schools' Online Certification Program
The perfect solution for professionals who need to balance work, family, and career building.
More than 4000 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).
|