Regex flags and safe testing

Published: 2026-07-20

What g, i, m, s, and u mean in JavaScript regex — and how to test patterns on pasted text without sending data to a server.

Regular expressions match patterns in text: emails (imperfectly), log lines, identifiers, or refactor targets. Modern JavaScript (and most languages) attach flags that change how ., ^, $, and Unicode behave. Testing regex on production logs is safer when the tester runs locally.

Common flags in JavaScript

Flag Name Effect
g global Find all matches, not just the first
i ignore case A matches a
m multiline ^ and $ match line boundaries, not only string ends
s dotAll . matches newline characters
u unicode \u{...} and Unicode-aware \b, \d
y sticky Match only at lastIndex

Literal syntax: /pattern/flags — e.g. /foo/gi.

Global flag and lastIndex

With g, repeated exec or matchAll advances lastIndex. Reusing the same regex object across unrelated strings without resetting can cause missed matches. In tests, create a fresh RegExp or set lastIndex = 0.

Multiline log parsing

Log files often span many lines. Without m, ^error only matches if the string starts with error. With m, it matches the start of each line — useful for grep-style filters in code.

Catastrophic backtracking

Some patterns (a+)+b on long strings can run for seconds (ReDoS). When testing user-supplied regex against large pasted input:

  • Prefer possessive or atomic groups where your engine supports them.
  • Cap input size in tools you ship.
  • Never run untrusted regex on your server against unbounded text.

Try it locally

The Regex Tester Studio runs match tests in your browser. Paste sample text, toggle flags, and inspect capture groups without uploading the string.

Related reading

All learn articles