Concept

Regex Flags in JavaScript: g, i, m, s, u, y

Flags modify regex behavior globally. g enables global matching, i makes it case-insensitive, m changes anchor behavior, s dots match newlines, u enables Unicode, y is sticky.

g — global

Without g, methods like String.match return only the first match. With g, they return all matches. String.replace and String.replaceAll use g to process every occurrence. Critical for extraction use cases.

i — case-insensitive

/hello/i matches 'Hello,' 'HELLO,' and 'hELLO.' The i flag applies to the whole expression — you can't flip it on for part of a regex without modern inline modifiers.

Case-insensitive extension match

Try this
/\.png$/i

Input

IMG_1234.PNG

Result

Match: .PNG

m — multiline

Changes ^ and $ to match at the start and end of each LINE in addition to the start/end of the string. Essential for log parsing and any line-by-line processing in a single input buffer.

s — dotall (single-line)

Without s, the dot . matches any character EXCEPT a newline. With s, it matches newlines too. The name is confusing — 'single-line' mode means the engine treats the input as one big line.

Without s — fails across lines

Try this
/<pre>.*</pre>/

Input

<pre>line1 line2</pre>

Result

No match

With s — spans newlines

Try this
/<pre>.*</pre>/s

Input

<pre>line1 line2</pre>

Result

Match: whole <pre> block

u and y

u enables full Unicode matching — \p{...} properties, surrogate pair handling, and stricter escape rules. Essential for any non-ASCII text. y (sticky) forces the match to start exactly at the lastIndex position of the regex object — useful for tokenizers that advance through input sequentially.

Related patterns

All reference guidesOpen the RegexPro tester →