Global

By default, as soon as a regular expression finds a match within a string, it stops looking. The global flag tells the regular expression to find all matches in the string, not just the first. To set the global flag, you place a g outside the closing forward slash of your regular expression: /[0-9]{4}/g finds all years in a string, not just the first one.

Get Learning to Program now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.