You have three possible values:
g : meaning global match.
i : meaning ignore case.
m : meaning match over multiple lines.
The g Flag
Consider the following statement:
var availableString = "I have a dog. You have a dog. She has a dog.";
Matching normally occurs for the first case in an available string. If your regexp is
var re = /dog/
then it is the "dog" in the first sentence that will be matched (seen in the available string). The "dog" in the second and third sentences will not be matched. If you want to match all the cases of the word "dog," then your regexp will have to be
var re = /dog/g
The g is a flag that means global match; that is, match all the occurrences of the regexp in the string. The question you may have now is "What do you gain with global matching?" As we go on, you will see the uses. For example, you may want to know the number of occurrences of the regexp in the available string. I will show you how to get this later.
With the constructor function you would have
var re = new RegExp("dog","g")
This statement is equivalent to the previous one.
The i Flag
By default, matching is case sensitive. To make it case insensitive, you have to use the i flag.
So if we have
var re = /send/
which is equivalent to
var re = new RegExg("send")
and then we also have
var availableString = "Click the Send button."
The following code will not produce a match:
var re = /send/;
var availableString = "Click the Send button.";
The regexp did not match the available string because the regexp has "send" where s is in lower case, but the available string has "Send" where S is in upper case. If you want this matching to be case insensitive, then your regexp will have to be
var re = /send/i
var re = new RegExg("send","i")
The following code will produce a match.
var re = new RegExp("send","i");
var availableString = " Click the Send button.";
Matching has occurred because we have made the regexp case insensitive, with the i flag.
We take a break here and continue in the next part of the series.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.