Pattern Flags in JavaScript

From the JavaScript Regular Expressions cheat sheet ยท Flags ยท verified Jul 2026

Pattern Flags

Modifiers that change how patterns work

javascript
g   Global - find all matches
i   Case insensitive
m   Multi-line mode
s   Dot matches newline
u   Unicode mode
y   Sticky mode
d   Has indices (with match indices)

// Using flags
/pattern/gi                       // Multiple flags
new RegExp('pattern', 'gi')       // With constructor

// Global flag (g)
'aaa'.match(/a/)     // ['a'] - first only
'aaa'.match(/a/g)    // ['a','a','a'] - all matches

// Case insensitive (i)
/hello/i.test('HELLO')            // true

// Multi-line (m)
/^line2/m.test('line1\nline2')   // true
/^line2/.test('line1\nline2')    // false

// Dot matches newline (s)
/.+/s.test('line1\nline2')        // matches all including \n
/.+/.test('line1\nline2')         // matches only 'line1'

// Unicode (u)
/\u{1F600}/u.test('๐Ÿ˜€')          // true (emoji)
/\p{Emoji}/u.test('๐Ÿ˜€')          // true (Unicode property)

// Sticky (y)
const sticky = /\d+/y
sticky.exec('123 456')   // ['123']
sticky.exec('123 456')   // null (must start at index 3)

// Has indices (d)
const re = /a(b)/d
const match = re.exec('ab')
// match.indices: [[0,2], [1,2]]
๐Ÿ’ก Global flag needed for methods like replaceAll and matchAll
โšก Unicode flag enables \p{} for Unicode properties
๐Ÿ“Œ Sticky flag matches only at lastIndex position
๐ŸŸข Combine flags as needed: /pattern/gim
flagsmodifiers
Back to the full JavaScript Regular Expressions cheat sheet