Core JavaScript 1.5 Guide:Working with Regular Expressions:Executing a Global Search, Ignoring Case, and Considering Multiline Input
From MDC
[edit] Advanced Searching With Flags
Regular expressions have four optional flags that allow for global and case insensitive searching. To indicate a global search, use the g flag. To indicate a case-insensitive search, use the i flag. To indicate a multi-line search, use the m flag. To perform a "sticky" search, that matches starting at the current position in the target string, use the y flag. These flags can be used separately or together in any order, and are included as part of the regular expression.
Support for the y flag was added in Firefox 3. The y flag fails if the match doesn't succeed at the current position in the target string.
To include a flag with the regular expression, use this syntax:
re = /pattern/flags
re = new RegExp("pattern", ["flags"])
Note that the flags are an integral part of a regular expression. They cannot be added or removed later.
For example, re = /\w+\s/g creates a regular expression that looks for one or more characters followed by a space, and it looks for this combination throughout the string.
<script type="text/javascript"> re = /\w+\s/g; str = "fee fi fo fum"; myArray = str.match(re); document.write(myArray); </script>
This displays ["fee ", "fi ", "fo "]. In this example, you could replace the line:
re = /\w+\s/g;
with:
re = new RegExp("\\w+\\s", "g");
and get the same result.
The m flag is used to specify that a multiline input string should be treated as multiple lines. If the m flag is used, ^ and $ match at the start or end of any line within the input string instead of the start or end of the entire string.