Detail the use of the global attribute in the JavaScript regular expression

  • 2020-06-15 07:37:47
  • OfStack

global is a read-only Boolean property of a regular expression object. It specifies whether a particular regular expression is globally matched. Otherwise it is created using the "g" attribute.
grammar


RegExpObject.global

Here is the details of the parameters:

NA

The return value:

Return "TRUE" if "g" modification is set, otherwise return "FALSE".

Example:


<html>
<head>
<title>JavaScript RegExp global Property</title>
</head>
<body>
<script type="text/javascript">
  var re = new RegExp( "string" );

  if ( re.global ){
   document.write("Test1 - Global property is set"); 
  }else{
   document.write("Test1 - Global property is not set"); 
  }
  re = new RegExp( "string", "g" );

  if ( re.global ){
   document.write("<br />Test2 - Global property is set"); 
  }else{
   document.write("<br />Test2 - Global property is not set"); 
  }
</script>
</body>
</html>

This will produce the following results:


Test1 - Global property is not set
Test2 - Global property is set


Related articles: