Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

RegExp flags

Flags change how matching works: g for all matches, i for case-insensitive, and more.

Available since: ES3

Syntax

/pattern/gimsuyd

Examples

The g flag finds every match, without it only the first:

console.log('a-b-c'.replace(/-/, '+'));
console.log('a-b-c'.replace(/-/g, '+'));

Output:

a+b-c
a+b+c

The i flag ignores case:

console.log(/hello/i.test('HELLO there'));

Output:

true

The m flag makes ^ and $ work for each line:

const text = 'one\ntwo\nthree';
 
console.log(text.match(/^\w+$/gm).join(','));

Output:

one,two,three

The s flag lets the dot match line breaks:

console.log(/a.b/.test('a\nb'), /a.b/s.test('a\nb'));

Output:

false true

Notes

Other flags: u handles Unicode and emoji correctly, y matches strictly at lastIndex, d adds group positions to the result, v is an extended Unicode mode. The regex.flags property returns the flags.

See also

In the tutorial

  • Regular expressions — Regular expressions in JavaScript for beginners: patterns, flags, character classes, testing, finding and replacing text.

Thanks for reading!

Donate to the site — we work hard for you.