Table of Contents
What Are Regular Expressions?
A Regular Expression (regex or regexp) is a sequence of characters that forms a search pattern. Under the hood, modern regex engines (such as PCRE, V8 RegExp, and Python's re) compile these strings into Deterministic or Non-Deterministic Finite Automata (DFA/NFA) to search, validate, and manipulate text with extreme efficiency.
Core Metacharacters & Character Classes
| Character | Meaning | Example |
|---|---|---|
^ | Asserts start of line/string | ^Hello matches "Hello world" |
$ | Asserts end of line/string | end$ matches "the end" |
\d / \D | Digit (0-9) / Non-digit | \d{4} matches "2026" |
\w / \W | Word character [a-zA-Z0-9_] / Non-word | \w+ matches variable names |
\s / \S | Whitespace (space, tab, newline) / Non-space | \s*=\s* matches flexible assignment |
[abc] | Character set (matches any one character) | [aeiou] matches vowels |
[^abc] | Negated character set | [^0-9] matches non-numbers |
Lookaheads & Lookbehinds: Zero-Width Assertions
Lookarounds allow you to match a pattern only if it is followed or preceded by another pattern without including that surrounding text in the match result:
- Positive Lookahead
(?=...):\d+(?=px)matches "100" in "100px". - Negative Lookahead
(?!...):foo(?!bar)matches "foo" only if NOT followed by "bar". - Positive Lookbehind
(?<=...):(?<=\$)\d+matches "50" in "$50". - Negative Lookbehind
(?<!...):(?<!-)foomatches "foo" only if not preceded by a hyphen.
Catastrophic Backtracking: Preventing Regex Denial of Service (ReDoS)
When an NFA regex engine evaluates nested quantifiers such as (a+)+$ against input like "aaaaaaaaaaaaaaaaaaaaX", it attempts every permutation of grouping before failing. For a 30-character string, this can require billions of backtracking steps, locking a CPU thread at 100% and crashing your server or freezing a browser tab.
Remedy: Avoid nesting quantifiers on overlapping character sets, use atomic grouping, or enforce hard input length bounds before evaluating regex.
Test, validate, and debug your regex patterns in real time using our local Regex Tester tool.
Written by Dhaval Joshi. Have feedback or questions? Contact us at support@freetoolkit.online.