global Property

Returns a Boolean value indicating the state of the global flag (g) used with a regular expression. Default is false. Read-only.


The required rgExp reference is an instance of a Regular Expression object.


The global property returns true if the global flag is set for a regular expression, and returns false if it is not.

The global flag, when used, indicates that a search should find all occurrences of the pattern within the searched string, not just the first one. This is also known as global matching.


The following example illustrates the use of the global property. If you pass "g" in to the function shown below, all instances of the word "the" are replaced with the word "a". Note that "The" at the beginning of the string is not replaced. This is because the initial letter is uppercase and, therefore, does not match the lowercase "t" in "the".

This function returns a string with a table that shows the condition of the properties associated with the allowable regular expression flags, g, i, and m. The function also returns the string with all replacements made.

function RegExpPropDemo(flag){
   if (flag.match(/[^gim]/))        //Check flag for validity.
     return("Flag specified is not valid");
   var r, re, s                    //Declare variables.
   var ss = "The man hit the ball with the bat.\n";
   ss += "while the fielder caught the ball with the glove.";
   re = new RegExp("the",flag);    //Specify the pattern to search for.
   r = ss.replace(re, "a");        //Replace "the" with "a".
   s = "Regular Expression property values:\n\n"
   s += "global  ignoreCase  multiline\n"
   if (re.global)                  //Test for global flag.
     s += " True     ";
     s += "False     ";
   if (re.ignoreCase)              //Test ignoreCase flag.
     s += " True  ";
     s += "False  ";
   if (re.multiline)               //Test multiline flag.
     s += "     True     ";
     s += "     False   ";
   s += "\n\nThe resulting string is:\n\n" + r;
   return(s);                      //Returns replacement string


Version 5.5

See Also

ignoreCase Property | multiline Property | Regular Expression Syntax

Applies To: RegExp Object