How do JavaScript regex flags change matching behavior?
Flags appended after the closing slash alter whether an expression stops after one hit, ignores letter casing, spans multiple lines, or treats Unicode code points as whole characters. When you test regular expressions in your browser, toggling the six flag pills below Pattern updates /pattern/flags live and reruns the match worker after an 80 ms debounce.
Every evaluation runs locally inside your browser tab without uploading logs, API keys, or configuration snippets to a remote server. The default (\w+)@([\w.-]+)\.(\w+) pattern and sample addresses let you inspect live highlighting and capture groups immediately upon opening the page.
Global, case-insensitive, multiline, and dotAll toggles
The default g (all matches) and i (ignore case) flags find every occurrence regardless of capitalization. Turning off g stops at the first match in both Matches and Replace, which is useful when testing single-record extraction.
Enabling m (^ $ per line) makes ^ and $ anchor at every line break instead of only the start and end of the entire Test text block. Enabling s (. matches newline) lets the dot wildcard match \n characters across multi-line log entries, as detailed in the MDN JavaScript Regular Expressions Guide.
Unicode u mode and sticky y anchors
Enabling u (Unicode) switches the engine to full code-point matching so emoji and \p{Letter} property escapes work accurately. Even when u is unchecked, the highlighter clamps slice boundaries so non-Unicode matches never split a UTF-16 surrogate pair in Test text.
The y (sticky) flag forces matching to continue strictly from lastIndex without skipping unmatched characters. If you write schedule expressions alongside log filters, you can also use cron expression explainer in your browser to verify recurring job timing.
Inspecting numbered and named capture groups in Matches
Parentheses inside a pattern record substrings for extraction, validation, and downstream replacement. As you type, the Matches panel lists every hit with its zero-based character offset (at <index>) and expands any captured subgroups underneath.
Reading $1, $2, and (?…) captures
Numbered groups appear in left-to-right opening-parenthesis order as $1, $2, and so on. Named groups written as (?<user>\w+) display their exact label instead of a number, making complex URL or log parsers much easier to audit.
Both numbered and named captures follow the ECMA-262 RegExp Objects Specification, which also governs positive and negative lookbehind assertions like (?<=@)\S+. When extracting structured API fields from logs, you can validate extracted JSON payloads with the JSON formatter right after verifying your pattern.
Alternating highlights and the 1,000-match cap
Inside Test text, consecutive matches alternate between two highlight shades so adjacent hits remain visually distinct. When a zero-length assertion such as \b or a lookahead matches at a boundary, a thin vertical bar marks the exact offset instead of a shaded span.
To prevent browser memory spikes on huge files, the worker caps collection at 1,000 matches (displaying 1000+ matches) and renders the first 200 entries in the Matches list. Every match displays its exact character index (at <index>) so you can locate hits quickly in long multi-line files.
Why does a Web Worker stop runaway patterns after one second?
Poorly structured quantifiers can trigger exponential backtracking that freezes a single-threaded browser tab for minutes. Running every evaluation inside an isolated Web Worker lets a 1,000 ms (1 second) watchdog terminate stalled runs cleanly and keep the page responsive.
Spotting catastrophic backtracking before deployment
Nested repeats such as (a+)+ or (a|aa)+ force backtracking NFA engines to test exponentially many paths when a string almost matches and then fails at the end. The OWASP ReDoS Prevention Guide documents how a 30-character input can lock up production Node.js servers.
Testing untrusted or complex patterns here reveals performance cliffs before they ship. When validating CSS or design tokens with hex patterns like #([0-9a-fA-F]{6}|[0-9a-fA-F]{3}), you can verify matched HEX and RGB values in the color converter.
Watchdog termination and automatic worker recovery
When a pattern exceeds 1,000 ms, the watchdog calls worker.terminate(), clears the stalled thread, and shows a clear hint to simplify nested repeats. As soon as you edit Pattern or Test text, a fresh worker spawns automatically with zero page reload required.
Invalid syntax such as an unclosed character class or trailing backslash reports the exact JavaScript RegExp error inline below the flag pills without clearing your Test text or Replacement template.
Live Replace previews with $1, $&, and named references
Checking Replace opens the Replacement input and renders the transformed text live in Result with a one-click Copy button. Unlike external scripts, you can test $1, $&, and $<name> substitutions interactively before pasting them into your codebase.
Running substitutions off the main thread relies on the MDN Web Workers API Reference, ensuring large find-and-replace previews never lag while typing. Use the table below as a quick reference for JavaScript replacement tokens and flag behavior.
| Token or flag | Syntax | Meaning in JavaScript RegExp | Practical example |
|---|---|---|---|
| Whole match | $& |
Inserts the entire matched substring | [$&] wraps hits in brackets |
| Numbered group | $1, $2 |
Inserts capture group 1 or 2 | $1 at $2 reformats email parts |
| Named group | $<name> |
Inserts (?<name>...) capture |
$<year>-$<month> reorders dates |
| Literal dollar | $$ |
Inserts a single $ character |
$$$1 prefixes a price |
| Global flag | g |
Replaces every match instead of first | Required for full-file rewrites |
| Multiline flag | m |
Applies ^ and $ per line |
Strips leading prefixes on each line |
Tip: After running a bulk find-and-replace on configuration or log files, paste the original and updated versions into Text diff to verify every changed line side by side.