User Guide
300 Using Regular Expressions
When the g flag is set, the Sting.match() method returns multiple matches, as follows:
var str:String = "she sells seashells by the seashore.";
var pattern:RegExp = /sh\w*/g;
// The same pattern, but this time the g flag IS set.
trace(str.match(pattern)); // output: she,shells,shore
The i (ignoreCase) flag
By default, regular expression matches are case-sensitive. When you set the i (ignoreCase)
flag, case sensitivity is ignored. For example, the lowercase
s in the regular expression does not
match the uppercase letter
S, the first character of the string:
var str:String = "She sells seashells by the seashore.";
trace(str.search(/sh/)); // output: 13 -- Not the first character
With the i flag set, however, the regular expression does match the capital letter S:
var str:String = "She sells seashells by the seashore.";
trace(str.search(/sh/i)); // output: 0
The i flag ignores case sensitivity only for the A–Z and a–z characters, but not for extended
characters such as
É and é.
The m (multiline) flag
If the m (multiline) flag is not set, the ^ matches the beginning of the string and the $
matches the end of the string. If the
m flag is set, these characters match the beginning of a line
and end of a line, respectively. Consider the following string, which includes a newline
character:
var str:String = "Test\n";
str += "Multiline";
trace(str.match(/^\w*/g)); // Match a word at the beginning of the string.
Even though the g (global) flag is set in the regular expression, the match() method matches
only one substring, since there is only one match for the
^—the beginning of the string. The
output is:
Test
Here is the same code with the m flag set:
var str:String = "Test\n";
str += "Multiline";
trace(str.match(/^\w*/gm)); // Match a word at the beginning of lines.
This time, the output includes the words at the beginning of both lines:
Test,Multiline