/pattern/g and the RegExp constructor. Use test() for a boolean check, match()/matchAll() to extract data, and replace() to transform text.JavaScript regex example
A small example is often the fastest way to understand how JavaScript regex works. The expression below checks whether a string contains exactly three digits.
const regex = /^\d{3}$/;
regex.test('123'); // true
regex.test('12A'); // falseThe anchors ^ and $ make the pattern validate the complete string instead of finding three digits anywhere inside it.
Regex literal or RegExp constructor?
Use a regex literal when the pattern is known in your source code. Use new RegExp() when part of the expression must be built dynamically. Remember that a JavaScript string adds another escaping layer.
const literal = /\d{3}/g;
const dynamic = new RegExp('\\d{3}', 'g');Both patterns search for three digits. The second version needs \\d in the JavaScript string so the regular expression receives \d.
test() for true or false
RegExp.prototype.test() is ideal for validation-style questions: “does this value match the rule?”
const username = /^[a-z][a-z0-9_]{2,15}$/i;
username.test('dev_2026'); // true
username.test('a!'); // falsematch() and matchAll() for extraction
Use match() for straightforward matches. When you need every occurrence together with capture groups, matchAll() is usually easier to work with.
const text = 'IDs: DEV-2026 and OPS-0001';
const regex = /([A-Z]{3})-(\d{4})/g;
for (const match of text.matchAll(regex)) {
console.log(match[0]); // DEV-2026, OPS-0001
console.log(match[1]); // DEV, OPS
console.log(match[2]); // 2026, 0001
}JavaScript regex groups and named groups
Capture groups let you keep meaningful pieces of a match. Named groups make the result easier to read when the pattern has several parts.
const id = /(?<team>[A-Z]{3})-(?<number>\d{4})/;
const match = 'DEV-2026'.match(id);
console.log(match.groups.team); // DEV
console.log(match.groups.number); // 2026This is also where the RegexArena Regex X-Ray becomes useful: the Playground can visually separate captured groups so you can see which part of the text each group matched.
Extract values with JavaScript regex
For structured text, regex can extract IDs, dates, tags, numbers, or other small fields. Keep the requirement narrow and test false positives as carefully as successful matches.
const input = 'Order #4821 total $150'; const order = input.match(/#(?<id>\d+)/); const total = input.match(/\$(?<amount>\d+)/); console.log(order.groups.id); // 4821 console.log(total.groups.amount); // 150
replace() for text transformation
Regex is useful for normalization as well as matching. For example, repeated whitespace can be collapsed into one space.
'regex is useful'.replace(/\s+/g, ' '); // 'regex is useful'
Capture groups can also be referenced in replacements when you need to reorder formatted data.
JavaScript regex flags
| Flag | Purpose | Typical use |
|---|---|---|
g | Global search | Find every occurrence |
i | Case-insensitive | Match DEV and dev |
m | Multiline anchors | Use ^/$ per line |
s | Dotall | Allow . to include line breaks |
u | Unicode-aware | Safer Unicode handling |
y | Sticky match | Match from lastIndex |
d | Match indices | Get match/group positions |
Common JavaScript regex examples
// Numbers only
/^\d+$/
// Simple email-shaped string
/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,}$/
// DEV- followed by four digits
/^DEV-\d{4}$/
// Extract all integers
/\d+/gThese are starting points, not universal validation rules. Requirements vary by project, especially for email addresses, URLs, dates, and international phone numbers.
Common JavaScript regex mistakes
- Forgetting anchors when you mean to validate the entire value.
- Using
gwith repeatedtest()calls without understandinglastIndex. - Forgetting to double-escape backslashes inside
new RegExp('...'). - Writing one huge pattern before defining positive and negative examples.
- Using regex where a parser or numeric/date conversion would be safer.
Test JavaScript regex online
Paste your own text into the RegexArena tester and inspect matches and capture groups visually. Then move to the game to practice the same concepts with positive and negative test cases.
Turn JavaScript Regex into practice
Use exercises 19, 20, 24, 28, and 29 to practice capture, identifiers, named groups, and extraction before returning to your code.
Practice JavaScript Regex