iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Flags (i / g / m / s / u)

Flags change how a regex matches. i case-insensitive, g global, m multi-line (so ^ / $ match line boundaries), s dot-matches-newline, u Unicode, y sticky.

Every common flag with a worked example

EXAMPLE
// i — case insensitive
/^hello$/i.test('Hello');         // true

// g — find all matches (used by replace / matchAll)
'aaa'.replace(/a/g, 'b');         // 'bbb'
[...'a-b-c'.matchAll(/-/g)].length; // 2

// m — multi-line: ^ and $ match line starts / ends
const log = `INFO ok\nERROR boom\nINFO done`;
log.match(/^ERROR.*$/m)?.[0];     // 'ERROR boom'

// s — dotall: . matches newlines
/<title>(.*)<\/title>/s.exec('<title>foo\nbar</title>')[1];   // 'foo\nbar'

// u — Unicode aware
/^\p{Letter}+$/u.test('café');     // true
/^.{5}$/u.test('🇦🇺café');         // true if you intended grapheme-ish

// y — sticky, matches AT lastIndex only
const re = /\w+/y;
re.lastIndex = 5;
re.exec('hello world');           // ['world']

// v — Unicode v2 (modern engines)
//      adds set notation, intersections, complement classes
/[\p{Letter}--[A-Z]]/v.test('a'); // true — letters minus uppercase ASCII

// Flag combinations
const pattern = new RegExp(query, 'gim');

// Inline flag modifiers (PCRE / Python / .NET)
//   (?i) — case insensitive from here on
//   (?ix) — ignore case + extended (allow whitespace + comments)
// JS doesn't support inline modifiers — set the flag on the literal instead.

Why it matters

Reach for the u flag on any regex that may see non-ASCII text. Without it, character classes, ., and length-based logic all do the wrong thing on emoji, CJK, and accented characters.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
/foo/i      // case-insensitive
/foo/g      // global (find all)
/foo/m      // multiline (^ $ per line)
/foo/s      // dotall (. matches \n)
/foo/u      // Unicode
/foo/y      // sticky (anchored at lastIndex)
Try it Yourself »

Exercise

Flag for case-insensitive matching.

/foo/

Discussion

Loading…