Glossary
Plain English
Cross-linked to tools

Regular Expression

Also known as: Regex, Regexp, Pattern matching

A regular expression (regex) is a small pattern language for describing the shape of strings — used to validate input, search inside text, extract data, and substitute matches across virtually every modern programming language.

Overview

A regex pattern is a string like '\d{3}-\d{4}' that describes another string ('three digits, dash, four digits'). Regex engines compile the pattern into a finite automaton and run it against input in linear time for most patterns.

The two big regex flavors are ECMAScript / JavaScript (what browsers run) and PCRE / Perl (what most CLIs and Python use). They share a common core — character classes, quantifiers, anchors, groups — but diverge on advanced features like lookbehind support, possessive quantifiers, and Unicode property escapes. Pasting a PCRE regex into JavaScript or vice versa is a frequent source of bugs.

Flags (g for global, i for case-insensitive, m for multiline, s for 'dot matches newline', u for Unicode) change matching behavior across the entire pattern.

Common questions about Regular Expression

What does the 'g' flag do?
The 'g' (global) flag tells the regex engine to return every match in the input string rather than stopping at the first. Without 'g', methods like String.prototype.match() return only the first match plus its capture groups.
Why does my regex hang the browser?
Catastrophic backtracking. Patterns with nested quantifiers like (a+)+ can take exponential time on certain inputs. Rewrite the pattern to avoid nested quantifiers, or use atomic groups (in PCRE) or possessive quantifiers (in some engines).
How do I match across newlines?
By default the dot ('.') does not match newline characters. Set the 's' (dotAll) flag to make it match every character including newlines. The 'm' (multiline) flag is different — it makes ^ and $ anchor to each line instead of the whole string.

Tools that work with Regular Expression

Regex Tester

Test JavaScript regular expressions with live match highlighting.

External references