When you specify a regular expression, it will by default only match the first occurrence that it encounters. It is possible to specify a pattern to match that can occur several times in a line. To match all of these, you can use the 'g' attribute. The 'g' stands for global matching.
There is one other attribute that allows the matching to be carried out in a case-insensitive manner. That is the 'i' attribute.
Both of these attributes are placed after the closing slash at the end of the pattern.
The case-insensitive match works like this:
/javascript/i
would match the following strings
JavaScript
javascript
JAVASCRIPT
JaVaScRiPt
The g attribute applied like this, would cause the pattern:
/0/g
to match every zero in the string:
'0100, 00123, "00067", 666000'
There is another attribute to control whether the pattern is applied to single lines or multiple lines. Because the regular expression is realized as an object, this is controlled by means of an object property accessed via the multi-line identifier, the letter 'm' in JavaScript 1.5 (shipped with Netscape 6.0)
Here is a list of the available attributes:
seq | Pattern | Description | JS |
---|---|---|---|
00 | g | Global match for every occurrence of the pattern throughout the line. | 1.2 |
01 | i | Case-insensitive matching. | 1.2 |
02 | ig | Case-insensitive global match. | 1.2 |
13 | m | Multiple lines to be processed. Available only in JavaScript 1.5 and mutually exclusive with the 's' attribute. | 1.5 |
15 | gm | Global match on multiple lines. | 1.5 |
15 | im | Case-insensitive matching on multiple lines. | 1.5 |
16 | igm | Case-insensitive global match on multiple lines. | 1.5 |
Prev | Home | Next |
RegExp pattern - alternation | Up | RegExp pattern - character class |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |