What is Regex?
A regular expression (regex) is a compact language for matching text patterns. You write a pattern with literals, character classes, quantifiers, and anchors; the engine finds matches in a string. Regex powers input validation, log parsing, search-and-replace, and syntax highlighting across languages.
Core concepts
Literals match exact text. `.` matches any character. `[a-z]` is a character class. `*`, `+`, and `?` are quantifiers. `^` and `$` anchor start and end. Parentheses create capture groups.
Flags
JavaScript flags include `g` (global, all matches), `i` (case-insensitive), `m` (multiline), `s` (dotall), `u` (unicode), and `y` (sticky). Flags change how the pattern is interpreted.
When to use regex
Use regex for format checks (email, URL), extracting tokens from logs, and bulk text transforms. For complex HTML or nested structures, prefer a proper parser over regex.