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

前端 未结 9 833

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

What is is the difference between /.+/g and /.+/?

9条回答
  •  萌比男神i
    2020-11-22 10:11

    g is the global search flag.

    The global search flag makes the RegExp search for a pattern throughout the string, creating an array of all occurrences it can find matching the given pattern.

    So the difference between /.+/g and /.+/ is that the g version will find every occurrence instead of just the first.

提交回复
热议问题