Find matches

2 snippets in Regular Expressions

Also written as find all matches

RXRegular Expressions

g - Global Flag

RX · Flags & Modifiers
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.

JS: match() - Find Matches

RX · String Operations with Regex
syntax
string.match(/pattern/)  or  string.match(/pattern/g)
example
// Without g: returns first match + groups
'Price: $42.99'.match(/(\$)(\d+\.\d{2})/)
// With g: returns all matches (no groups)
'$42.99 and $18.50'.match(/\$\d+\.\d{2}/g)
output
Without g: ["$42.99", "$", "42.99"]
With g: ["$42.99", "$18.50"]

Note Behavior changes completely depending on the g flag. Without g, you get detailed info (groups, index). With g, you get a flat array of all matches but lose group details. Use matchAll for both multiple matches AND group info.