Regex Flags Explained (g, i, m, s, u, y)

A regex flag (also called a modifier) changes how the engine applies your pattern. In JavaScript, you append flags after the closing slash: /pattern/flags.

g — global

Without g, the engine stops at the first match.

"hello hello".match(/hello/)   // ["hello"] — first match only
"hello hello".match(/hello/g)  // ["hello", "hello"] — all matches

Use g any time you want every occurrence, not just the first. Also required when calling String.prototype.replaceAll with a regex.

i — case insensitive

/hello/i.test("Hello World")  // true
/hello/.test("Hello World")   // false

Saves you from writing [Hh][Ee][Ll][Ll][Oo]. Simple, but easy to forget.

m — multiline

By default, ^ and $ match the start and end of the whole string. With m they match the start and end of each line.

/^hello/m.test("world\nhello")  // true
/^hello/.test("world\nhello")   // false

You need m when your string contains newlines and you want ^/$ to work per-line rather than per-string.

s — dotAll

Normally . matches any character except newlines. The s flag removes that exception.

/start.end/s.test("start\nend")  // true
/start.end/.test("start\nend")   // false

Useful when matching content that can span multiple lines — HTML tags with line breaks, log entries, and similar.

u — unicode

Enables full Unicode support: \u{…} escapes, Unicode property classes (\p{…}), and correct handling of surrogate pairs.

/\u{1F600}/u.test("😀")  // true
/\u{1F600}/.test("😀")   // false

Always use u when input might contain emoji or non-ASCII characters. Without it, . splits some emoji across two characters and \u{…} syntax is a syntax error.

y — sticky

The y flag matches only at the exact position stored in lastIndex, not anywhere in the string. It's used for incremental parsing where you advance through the input chunk by chunk.

const re = /\d+/y;
re.lastIndex = 4;
re.exec("abc 42 foo")  // matches "42" because lastIndex === 4

Combining flags

Flags compose — just concatenate them:

const re = /hello/gi;
"Hello hello HELLO".match(re)  // ["Hello", "hello", "HELLO"]

Order doesn't matter: /gi and /ig behave identically.

Test flags live

Open the regex tester and toggle flags in real time. You'll see exactly which parts of your input match — and which ones don't — as you add or remove each flag.

For common patterns to test with, see common regex patterns. For the difference between greedy and lazy quantifiers, see regex greedy vs lazy.

Got a config file to check?

Open the config toolkit →