regexhelper
Test a pattern. Copy a ready-made one. No signup.

Regex Flags

Flags (or modifiers) change how the whole pattern behaves. Six matter most. Here is what each does, with an example and the cross-language differences.

The flags

FlagNameEffect
gGlobalFind all matches, not just the first (JavaScript)
iIgnore case/cat/i matches CAT
mMultiline^ and $ match at every line break
sDotall / single-line. also matches newline characters
uUnicodeEnables \p{...} and correct handling of astral chars
yStickyMatch only at lastIndex, no scanning ahead (JS)

How to set them

LanguageSyntax
JavaScript/pattern/gim or new RegExp('pattern','gi')
Pythonre.compile(r'pattern', re.I | re.M)
Inline (PCRE/Python)(?im)pattern at the start
Scoped inline(?i:abc) — case-insensitive for that group only

Multiline vs dotall — the common mix-up

They are not the same flag:

  • m changes what ^ and $ mean — they anchor to each line instead of the whole string.
  • s changes what . means — it starts matching newlines too.

Confusingly, some engines call s the "single-line" flag, which is the opposite name to what it does. Judge by behavior, not the label.

Why g matters in JavaScript

Without g, String.replace replaces only the first match and matchAll throws. With g, a regex also keeps a stateful lastIndex — reusing the same object across .test() calls can give surprising results, so create a fresh regex when in doubt.

FAQ

What is the difference between the m and s flags?

m (multiline) makes ^ and $ match at line breaks. s (dotall) makes the dot match newline characters too. They are independent and often used together.

Does Python have a global flag like JavaScript's g?

No. In Python you choose the behavior by the function: re.findall and re.finditer return all matches, while re.search returns the first. There is no per-pattern global flag.

Why does my /g regex behave differently on repeated calls?

A global regex tracks lastIndex between calls to .test() and .exec(), so the next call resumes where the previous one stopped. Reset lastIndex to 0 or build a new RegExp for a clean start.