The /g always means global. It means that the regex engine tries to match your regex as many times as possible within the text you are searching. It starts searching again at the next character after the last character that was used for the previous match. For example, if you have this text:

ABCDEABCDEABCDE

This non-global expression:

/C/

Would return a 1 to indicate that your regex matches somewhere in the searched text.

But, this global expression:

/C/g

Would return a 3 to indicate that your regex matched 3 times in the searched text.



Greedy is another term in regex that means the regex engine tries to take as many characters as it can, while still matching your regex. The regex engine can also run in non-greedy mode, where it takes as few characters as possible while still matching your regex. For example, if you had this text:

ABCDEABCDEABCDE

And you used this greedy regex:

/(B.*D)/

You should get this result:

BCDEABCDEABCD

But, if you used this non-greedy regex:

/(B.*?D)/

You should get this result:

BCD


-genius_at_work