Cryonel

Regular Expression Flags Reference

Regex flags change how an engine interprets or iterates a pattern. Names and behavior vary, so test the exact engine and API used in production.

Common flagMeaningImportant note
iCase-insensitive matchingUnicode case folding differs by engine.
mMultiline anchorsChanges ^ and $ behavior, not dot.
sDot-allAllows dot to match line terminators.
gGlobal iteration in JavaScriptCan make RegExp objects stateful through lastIndex.
uUnicode-aware mode in JavaScriptChanges parsing and code-point handling.
ySticky matching in JavaScriptRequires a match at the current lastIndex.
xExtended/free-spacing in engines such as PCREWhitespace and comments gain special treatment.

Case-insensitive mode

The i flag performs case-insensitive matching under engine-specific rules. ASCII examples are straightforward, but Unicode case mapping can be context-dependent and is not identical across platforms. Do not use a casual case-insensitive regex as the only normalization rule for usernames, domains, or security identifiers. Define canonicalization separately.

Multiline versus dot-all

Multiline mode changes anchors so ^ and $ can match around line boundaries in addition to input boundaries. Dot-all mode changes . so it includes line terminators. These behaviors are independent. Use explicit input anchors supported by the engine when the whole value must match, especially for validation.

Global and sticky iteration

JavaScript's g flag finds successive matches and updates lastIndex. Reusing the same RegExp object with methods such as test can therefore alternate results if state is not reset. The y flag also uses lastIndex but requires the next match to begin exactly there, which is useful for tokenizers. Other languages put “find all” behavior in the calling API rather than a pattern flag.

Unicode handling

Without the engine's Unicode mode, a character outside the Basic Multilingual Plane may be processed as multiple code units. Character properties, word boundaries, case folding, and dot behavior differ across engines. Use supported Unicode property escapes when needed and test emoji, combining marks, non-Latin scripts, and malformed input.

Extended mode

PCRE and several other engines offer a free-spacing or extended mode, commonly x, that ignores unescaped pattern whitespace and permits comments outside character classes. This makes complex patterns readable but changes the meaning of literal spaces and hash characters. JavaScript does not provide the same flag directly.

Portability and safety

A literal written as /pattern/g is language syntax, not a universal wire format. Escaping also passes through string-literal rules before the regex parser. When moving a pattern, verify delimiter rules, flags, Unicode semantics, lookbehind, named groups, and replacement syntax. Bound input and avoid catastrophic backtracking; flags do not make a vulnerable pattern safe.

Frequently Asked Questions

Does m make dot match newline?

No. Use dot-all mode for that behavior.

Is g universal?

No. Many APIs express repeated matching in the method call.

Are flags portable?

No. Test the exact engine and language.

Related Tools and Guides