Regex in JavaScript
JavaScript uses the built-in RegExp object for regular expressions. Patterns can be created with literal syntax (/pattern/flags) or the RegExp constructor. The engine supports lookaheads, lookbehinds, named capture groups, and Unicode property escapes.
Code Examples
Test if a string matches a pattern
const regex = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$/i;
const isValid = regex.test("user@example.com");
console.log(isValid); // trueThe test() method returns true or false. It's the fastest way to check whether a pattern matches anywhere in a string.
Extract all matches with matchAll()
const text = "Prices: $12.99, $5.50, $120.00";
const regex = /\$(\d+\.\d{2})/g;
for (const match of text.matchAll(regex)) {
console.log(match[0], "->", match[1]);
}
// $12.99 -> 12.99
// $5.50 -> 5.50
// $120.00 -> 120.00matchAll() returns an iterator of all matches including capture groups. Requires the g flag. Available in ES2020+.
Named capture groups
const dateRegex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const match = "2026-03-08".match(dateRegex);
console.log(match.groups.year); // "2026"
console.log(match.groups.month); // "03"
console.log(match.groups.day); // "08"Named groups use (?<name>...) syntax. Access them via match.groups.name instead of numeric indices — much cleaner for complex patterns.
Replace with a callback function
const text = "hello world"; const result = text.replace(/\b\w/g, (char) => char.toUpperCase()); console.log(result); // "Hello World"
String.replace() accepts a function as the second argument. The function receives each match and can return a dynamic replacement.
Split a string with regex
const csv = "one, two ,three, four"; const parts = csv.split(/\s*,\s*/); console.log(parts); // ["one", "two", "three", "four"]
String.split() accepts a regex, which is useful for splitting on variable-width delimiters like commas with optional whitespace.
Lookahead and lookbehind
// Positive lookahead: match digits followed by "px" "12px 3em 100px".match(/\d+(?=px)/g); // ["12", "100"] // Positive lookbehind: match digits preceded by "$" "$50 and $120".match(/(?<=\$)\d+/g); // ["50", "120"] // Negative lookahead: match digits NOT followed by "px" "12px 3em 100px".match(/\d+(?!px)/g); // ["1", "3", "10"]
Lookaheads (?=...) and lookbehinds (?<=...) match a position without consuming characters. Negative variants use (?!...) and (?<!...).
Note
JavaScript regex literals use forward slashes: /pattern/flags. The RegExp constructor takes a string, so backslashes must be doubled: new RegExp('\\d+'). The u flag enables full Unicode support including \p{...} property escapes. The v flag (ES2024) adds set notation and improved Unicode handling.
Regex in Other Languages
Frequently Asked Questions
What is the difference between test() and match()?
test() returns a boolean (true/false) and is faster when you only need to know if a pattern matches. match() returns an array with the matched text, capture groups, and index — use it when you need the actual match data.
How do I escape special regex characters in JavaScript?
Use a backslash before special characters: \. \* \+ \? \( \) \[ \] \{ \} \^ \$ \| \\. For dynamic patterns, escape user input with: str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').
What flags are available in JavaScript regex?
g (global), i (case-insensitive), m (multiline), s (dotAll — makes . match newlines), u (Unicode), y (sticky — matches at lastIndex), d (hasIndices — ES2022, provides start/end indices for groups), and v (unicodeSets — ES2024, extends u with set notation).
Why does my regex match differently in JavaScript than in Python?
JavaScript and Python use different regex engines. Key differences: JS requires double escaping in RegExp strings, JS lookbehinds must be fixed-length in older engines (variable-length supported in V8 since 2020), and Python has features like atomic groups and possessive quantifiers (3.11+) that JS lacks.
Want to test a JavaScript regex pattern? Our regex tester runs JavaScript's native RegExp engine in your browser — paste your pattern and see matches in real time.
← Open the Regex Tester