Technology · JavaScript
JavaScript Regular Expressions
Learn regex patterns, flags, exec, test, match, and replace for powerful string processing.
TL;DR
- 01Create regex patterns with forward slashes or the RegExp constructor.
- 02Use test() and match() to check patterns and extract matches.
- 03Use replace() with regex to transform strings globally.
Creating Patterns
- Create a regex with forward slashes for literal patterns.
const pattern = /hello/; pattern.test("hello world"); // true - Use the RegExp constructor for dynamic patterns.
const word = "hello"; const pattern = new RegExp(word); - Character classes match any single character from a set.
/[aeiou]/ matches any vowel /[0-9]/ matches any digit /[a-z]/ matches any lowercase letter - Use . to match any character except newline.
/h.llo/ matches "hello", "hallo", "h9llo" - Anchors match positions, not characters.
/^hello/ matches "hello" at start /world$/ matches "world" at end
Quantifiers
- Use * for zero or more matches of the previous element.
/a*b/ matches "b", "ab", "aab", "aaab" - Use + for one or more matches.
/a+b/ matches "ab", "aab" but not "b" - Use ? for zero or one match.
/a?b/ matches "b", "ab" but not "aab" - Use {n} for exactly n matches, {n,m} for a range.
/a{3}/ matches "aaa" /a{2,4}/ matches "aa", "aaa", or "aaaa" - Use {n,} for n or more matches.
/a{2,}/ matches "aa", "aaa", "aaaa"...
Flags and Methods
- Use the i flag for case-insensitive matching.
/hello/i.test("HELLO"); // true - Use the g flag to find all matches, not just the first.
"hello hello".match(/hello/g); // ["hello", "hello"] - Use the m flag for multiline mode where ^ and $ match line breaks.
/^line/m matches "line" at start of any line - The test() method returns true or false.
/[0-9]/.test("abc123"); // true - The exec() method returns match details or null.
/(\w+)@(\w+)/.exec("user@example.com"); // ["user@example.com", "user", "example"]
Matching and Replacing
- Use match() to find all matching strings.
"hello world hello".match(/hello/g); // ["hello", "hello"] - Use matchAll() for detailed match information with groups.
const matches = "a1b2c3".matchAll(/(\w)(\d)/g); for (const m of matches) console.log(m[1], m[2]); - Use replace() to change matched strings.
"hello world".replace(/hello/, "hi"); // "hi world" - Use replaceAll() to replace every match globally.
"hello hello".replaceAll(/hello/, "hi"); // "hi hi" - Use a function in replace() to compute replacements.
"hello world".replace(/\w+/, (m) => m.toUpperCase()); // "HELLO world"
Common Patterns
- Email validation pattern.
/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test("user@example.com"); - Phone number pattern.
/^\d{3}-\d{3}-\d{4}$/.test("123-456-7890"); - URL validation pattern.
/^https?:\/\/.+/.test("https://example.com"); - Extract numbers from a string.
"Price: $19.99".match(/\d+\.?\d*/g); // ["19.99"] - Remove whitespace from a string.
" hello world ".replace(/\s+/g, " ").trim();
Tip: Use online regex testers like regex101.com to build and debug patterns before using them in code.
Warning: The g flag affects the lastIndex property of regex objects, so reusing them can cause bugs — create new regex for each use or reset lastIndex to 0.