Overview

Regex flags modify how an engine interprets the pattern and the input. The same flag letter usually means the same thing across engines, but the API differs: Python uses re.IGNORECASE or the inline (?i) flag; JavaScript uses a trailing /gi literal. This card lists flags, anchors, quantifiers, and zero-width assertions. For full pattern vocabulary see regex-patterns.

Flags by language

Flag letterPython constantJSEffect
ire.IGNORECASE/iCase-insensitive match
mre.MULTILINE/m^ and $ match start/end of each line, not the string
sre.DOTALL/s. matches newline characters
xre.VERBOSEN/AIgnore unescaped whitespace and # comments in pattern
gN/A (findall)/gGlobal: find all matches, not just the first
uDefault in Python 3/uUnicode mode; required for \p{} in JS
Are.ASCIIN/A\w, \d, \s match ASCII only (Python default is Unicode)
Lre.LOCALEN/ALocale-aware \w etc.; deprecated in Python 3

Combine flags: re.compile(r"pattern", re.I | re.M) in Python; /pattern/gim in JS.

Inline flags in the pattern string apply from that point: (?i)foo matches foo, FOO, Foo. Inline flags are useful when the regex is passed as a string and you cannot set the flags argument.

Anchors

Anchors are zero-width: they match a position, not a character.

AnchorMatchesre.M effect (Python/JS)
^Start of stringStart of each line
$End of string (before optional final \n)End of each line
\AAbsolute start of stringNot affected by M
\ZAbsolute end of stringNot affected by M
\bWord boundary (between \w and \W)Not affected by M
\BNon-word boundaryNot affected by M

Use \A and \Z when validating whole strings and you cannot guarantee the re.MULTILINE flag is absent.

Quantifiers

QuantifierMeaningExampleMatches
?0 or 1 (greedy)colou?rcolor, colour
*0 or more (greedy)a*bb, ab, aab
+1 or more (greedy)a+bab, aab
{n}Exactly n\d{4}exactly 4 digits
{n,}n or more\d{2,}2 or more digits
{n,m}n to m (greedy)\d{2,4}2, 3, or 4 digits
??0 or 1 (lazy)<.+?>shortest match
*?0 or more (lazy)".*?"shortest quoted string
+?1 or more (lazy)<.+?>non-greedy HTML tag
{n,m}?Lazy bounded\d{2,4}?prefer 2, stop there
+ + (?=...)Possessive (Python 3.11+ atomic)no backtrack into group

Greedy quantifiers grab as much as possible and backtrack; lazy quantifiers grab as little as possible and expand. Lazy matching is not always faster; it still backtracks.

Lookahead and lookbehind

Zero-width assertions match without consuming characters.

SyntaxNameMatches if
(?=...)LookaheadPattern ahead matches
(?!...)Negative lookaheadPattern ahead does not match
(?<=...)LookbehindPattern behind matches
(?<!...)Negative lookbehindPattern behind does not match

Examples:

ExpressionMatches
\d+(?= dollars)Digits followed by ” dollars” (digits only, not the word)
\d+(?! dollars)Digits NOT followed by ” dollars”
(?<=\$)\d+Digits preceded by $ (digits only)
(?<!\$)\d+Digits NOT preceded by $
(?<=\bpre)\w+Word part after “pre” prefix

Python lookbehind must be fixed-width. (?<=ab+) raises an error; (?<=ab) works.

Named groups and backreferences

SyntaxPythonJSMeaning
Named capture(?P<name>...)(?<name>...)Named group
Named backref(?P=name)\k<name>Reference named group in pattern
Non-capturing(?:...)(?:...)Group without capture
Alternationcat|dogcat|dogEither alternative

Access named groups: m.group("name") in Python; m.groups.name in JS with /d flag or named group syntax.

Common gotchas

  • re.MULTILINE changes ^/$ but not .; you still need re.DOTALL for . to match newlines.
  • In Python, raw strings r"..." prevent double-escaping. "\n" is a newline; r"\n" is backslash-n for the regex engine.
  • JavaScript’s global /g flag maintains a lastIndex state on the regex object. Reusing the same compiled regex in a loop without resetting lastIndex produces incorrect results.
  • \b is a word boundary between \w and \W. In Unicode mode, \w includes non-ASCII letters, so \b positions shift compared to ASCII mode.
  • Lookbehind in Python must be fixed-width. Use re2 or restructure the pattern if you need variable-length lookbehind.
  • Catastrophic backtracking happens with nested quantifiers like (a+)+. Test ambiguous patterns on adversarial input before deploying in a server context.