RX

Flags & Modifiers

Regular Expressions · 7 entries

g - Global Flag

syntax
JS: /pattern/g    Python: (default for findall/sub)
example
JS:  'aaa'.match(/a/g)
JS:  'aaa'.match(/a/)   // without g
Py:  re.findall(r'a', 'aaa')  # global by nature
output
With g: ["a", "a", "a"]
Without g: ["a"] (first match only)
Python findall: ['a', 'a', 'a']

Note In JS, the g flag makes match() return all matches instead of just the first. Without g, match() returns the first match with capture groups. Python's re.findall and re.sub are inherently global. Use re.search for first-match-only behavior in Python.

i - Case-Insensitive Flag

syntax
JS: /pattern/i    Python: re.IGNORECASE or re.I
example
JS:  /hello/i.test('Hello World')   // true
Py:  re.search(r'hello', 'Hello World', re.IGNORECASE)
output
Matches 'Hello' despite different casing

Note Affects all literal characters and character classes in the pattern. [a-z] with the i flag will also match uppercase letters. Be aware that case-folding in Unicode can be surprising -- the German sharp s (ss) may match SS in some engines.

m - Multiline Flag

syntax
JS: /pattern/m    Python: re.MULTILINE or re.M
example
JS:  'apple\nbanana\ncherry'.match(/^\w+/gm)
Py:  re.findall(r'^\w+', 'apple\nbanana\ncherry', re.M)
output
["apple", "banana", "cherry"]

Note Changes ^ and $ from matching string start/end to matching line start/end. Does NOT change the behavior of \A and \Z in Python, nor does it affect how the dot treats newlines. Combine with g in JS for all line-start matches.

s - Dotall / Single-Line Flag

syntax
JS: /pattern/s    Python: re.DOTALL or re.S
example
JS:  'line1\nline2'.match(/line1.line2/)    // null
JS:  'line1\nline2'.match(/line1.line2/s)   // matches
Py:  re.search(r'line1.line2', 'line1\nline2', re.DOTALL)
output
Without s: no match (dot skips \n)
With s: "line1\nline2"

Note The s flag makes the dot (.) match newline characters too. Before this flag existed, the workaround was [\s\S] to match any character including newlines. The s flag was added to JS in ES2018.

u - Unicode Flag

syntax
JS: /pattern/u    Python: (Unicode by default in Python 3)
example
JS:  /^.$/u.test('[U+D83D][U+DE00]')    // true (emoji as one char)
JS:  /^.$/.test('[U+D83D][U+DE00]')     // false (two UTF-16 surrogates)
JS:  /\p{Emoji}/u.test('Hi')    // true (enables \p{} syntax)
output
With u: emoji treated as single character
Without u: emoji seen as two surrogate code units

Note In JS, the u flag enables full Unicode matching: surrogate pairs are treated as single code points, and Unicode property escapes (\p{...}) become available. Python 3 handles Unicode by default. Always use the u flag in JS when dealing with international text or emoji.

y - Sticky Flag (JS Only)

syntax
JS: /pattern/y
example
JS:  const re = /\d+/y;
     re.lastIndex = 4;
     re.exec('abc 123 xyz')   // ["123"] (starts exactly at index 4)
     re.exec('abc 123 xyz')   // null (index 7 is a space, not a digit)
output
First exec: "123" at index 4
Second exec: null (must match exactly at lastIndex)

Note The y flag forces the match to start at lastIndex, unlike g which searches forward from lastIndex. Useful for building tokenizers/lexers where you need sequential, contiguous matching. No Python equivalent -- Python's re.match already anchors to the start.

x - Verbose / Free-Spacing (Python)

syntax
Python: re.VERBOSE or re.X
example
Py:  pattern = re.compile(r'''
       \d{4}    # year
       -        # separator
       \d{2}    # month
       -        # separator
       \d{2}    # day
     ''', re.VERBOSE)
     pattern.search('2026-04-04')
output
Matches "2026-04-04"

Note Allows whitespace and comments in the pattern for readability. Spaces and # comments are ignored unless escaped or inside a character class. No native JS equivalent, but you can concatenate strings or use template literals to achieve similar readability.