What Is the Meaning of the 'G' Flag in Regular Expressions

What is the meaning of the 'g' flag in regular expressions?

g is for global search. Meaning it'll match all occurrences. You'll usually also see i which means ignore case.

Reference: global - JavaScript | MDN

The "g" flag indicates that the regular expression should be tested against all possible matches in a string.

Without the g flag, it'll only test for the first.

Additionally, make sure to check cchamberlain's answer below for details on how it sets the lastIndex property, which can cause unexpected side effects when re-using a regex against a series of values.

Why does the 'g' flag change the result of a JavaScript regular expression?

In JavaScript, regular expression objects have state. This matters when the g flag ("global") is applied to them, and sometimes applies in odd ways. This state is the index where the match last occurred, which is the regex's .lastIndex property. When you call exec or test on the same regex object again, it picks up from where it left off.

What's happening in your example is for the second call, it's picking up where it left off the last time, and so it's looking starting after the 10th character in the string — and doesn't find a match there, because there's no text there at all (and even if there were, the ^ assertion wouldn't match).

We can see what's happening if we look at the lastIndex property:

var reg = new RegExp("^19[-\\d]*","g");snippet.log("Before first test: " + reg.lastIndex);snippet.log(reg.test('1973-02-01')); //return truesnippet.log("Before second test: " + reg.lastIndex);snippet.log(reg.test('1973-01-01')); //return falsesnippet.log("After second test: " + reg.lastIndex);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --><script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

What does the regular expression /_/g mean?

The regex matches the _ character.

The g means Global, and causes the replace call to replace all matches, not just the first one.

What's the meaning of /gi in a regex?

g modifier: global. All matches (don't return on first match)

i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

In your case though i is immaterial as you dont capture [a-zA-Z].

For input like !@#$ if g modifier is not there regex will return first match !See here.

If g is there it will return the whole or whatever it can match.See here

Why does a RegExp with global flag give wrong results?

A RegExp object with the g flag keeps track of the lastIndex where a match occurred, so on subsequent matches it will start from the last used index, instead of 0. Take a look:

var query = 'Foo B';
var re = new RegExp(query, 'gi');
console.log(re.lastIndex);

console.log(re.test('Foo Bar'));
console.log(re.lastIndex);

console.log(re.test('Foo Bar'));
console.log(re.lastIndex);

What do i and g mean at the end of PCRE regex?

i, g are flags (or mode).

  • i means Ignore case. /[a-z]/i matches both lower case, upper case alphabet character.
  • g means globals match. Without this flag, some operation matches/replace only once.

See Regex Tutorial - Turning Modes On and Off for Only Part of The Regular Expression

What is the meaning of (/^\s+|\s+$/gm) in JavaScript?

It is a regular expression search that matches two alternative patterns:

/^\s+|\s+$/gm

/ Regex separator

First Alternative ^\s+

  • ^ asserts position at start of a line
  • \s+ matches any whitespace character (equal to [\r\n\t\f\v ])
  • + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)

Second Alternative \s+$

  • \s+ matches any whitespace character (equal to [\r\n\t\f\v ])
  • + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
  • $ asserts position at the end of a line

Global pattern flags

  • g modifier: global. All matches (don't return after first match)
  • m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

You can read more details on regex101.com.

Function explanation

The function call return x.replace(/^\s+|\s+$/gm,''); searches for any spaces from the beginning of the string and from the end of string.
If found then it is replaced by empty string ''.
Simply said it does the trim whitespace characters:

  • \n carriage return (ASCII 13)
  • \r line-feed (newline) character (ASCII 10)
  • \t tab character (ASCII 9)
  • \f form-feed character (ASCII 12)
  • \v any vertical whitespace character

javascript regex matches with g flag but not y flag

Adding the global flag to the sticky flag doesn't help either.

A regular expression defined as both sticky and global ignores the
global flag.

The "y" flag indicates that it matches only from the index indicated
by the lastIndex

You must set the lastIndex property of the regex, it defaults to 0, and there isn't any ( at that index, that's why nothing is matched. The ( appears at index 5.

let str = '\\[\\]\\(\\)\\{\\}\\<\\>';

let reg = /\(/y; // No need for new RegExp

reg.lastIndex = str.indexOf('('); // 5
reg.test(str); // True

More on sticky flag here:



Related Topics



Leave a reply



Submit