JS:'aaa'.match(/a/g)JS:'aaa'.match(/a/)// without gPy: 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.
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.
Frequently asked questions
How do you find matches?
Regular Expressions covers this with 2 copy-ready snippets on this page. The "g - Global Flag" snippet in Regular Expressions uses `JS: /pattern/g Python: (default for findall/sub)`.
Which pattern does the Regular Expressions example use?
The "g - Global Flag" snippet uses `JS: /pattern/g Python: (default for findall/sub)`, from the Flags & Modifiers section of the Regular Expressions cheat sheet.
What other Regular Expressions snippets are shown for "find matches"?
Besides "g - Global Flag", this page also shows "JS: match() - Find Matches".
Is there anything to watch out for?
Yes. For "g - Global Flag": 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.