Flags

A regular expression may have optional flags, which affect the search.

In JavaScript, there are three flags:

g
Find all matches.
i
Case-insensitive search
m
Enable multiline mode.

A flag is appended after the pattern, like /.../g.

A regexp without global flag returns only first match:

alert( "123".match( /\d/ ))  // '1'

But if the global flag is used, all matches can be found:

alert( "123".match( /\d/g ))  // '1', '2', '3'

Multiple flags are possible. For example, find all matches and ignore the case:

alert( "Smile a smile".match( /SMILE/gi ))  // 'Smile', 'smile'

The multiline flag is covered later on.

Tutorial

Donate

Donate to this project