javascript regular expression Description
regular expression | Firefox/Netscape/NN 4 IE 4 ECMA 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
A regular expression object is an instance of the RegExp object. Each regular expression object consists of a pattern that is used to locate matches within a string. Patterns for a regular expression can be simple strings or significantly more powerful expressions that use a notation that is essentially a language unto itself. The implementation of regular expressions in JavaScript 1.2 is very similar to the way they are implemented in Perl. You can read more about these concepts in books covering JavaScript 1.2 or later. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
To create a regular expression object, surround the pattern with forward slashes, and assign the whole expression to a variable. For example, the following statement creates a regular expression with a pattern that is a simple word: var re = /greet/; |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The re variable can then be used as a parameter in a variety of methods that search for the pattern within some string (you may also use an expression directly as a method parameter, rather than assigning it to a variable). |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Regular expression notation also consists of a number of metacharacters that stand in for sometimes complex ideas, such as the boundary on either side of a word, any numeral, or one or more characters. For example, to search for the pattern of characters shown above but only when the pattern is a word (and not part of a word such as greetings), the regular expression notation uses the metacharacters to indicate that the pattern includes word boundaries on both sides of the pattern: var re = /\bgreet\b/; |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The following table shows a summary of the regular expression notation used in JavaScript 1.2. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
When you create a regular expression, you may optionally wire the expression to work globally (as you probably do if the regular expression is doing a search-and-replace operation with a method, and your goal is a "replace all" result) and to ignore case in its matches. The modifiers that turn on these switches are the letters g and i. They may be used by themselves or together as gi. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Once you have established a pattern with the regular expression notation, all the action takes place in the regular expression object methods and the String object methods that accept regular expression parameters. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Properties | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Methods | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Creating a regular expression Object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var regExpressionObj = /pattern/ [g | i | gi]; var regExpressionObj = new RegExp(["pattern", ["g" | "i" | "gi"]]); |
Powered by Linode.