Regular expressions (regex) in JavaScript provide powerful methods to search, match, and manipulate strings based on patterns. The most commonly used regex methods are applied either on strings or on regex objects.

| Method | Type | Description |
|---|---|---|
test() | Regex | Tests if a pattern exists in a string; returns true or false. |
exec() | Regex | Returns detailed match info as an array or null if no match. |
match() | String | Returns match info from a string using a regex. |
matchAll() | String | Returns an iterator for all matches with detailed info. |
replace() | String | Replaces matched substrings using a regex pattern. |
search() | String | Returns the index of the first match or -1 if none. |
split() | String | Splits a string into an array using a regex as the delimiter. |
Regex Object Methods
- test(): Quickly checks if a pattern exists. Ideal for conditional checks.
- exec(): Returns an array with match details such as matched text, capturing groups, and index.
📌 Deep Dive: test() and exec()
const regex = /\d+/;
const str = "Order number: 12345";
console.log(regex.test(str));
// true, since digits are found
const match = regex.exec(str);
console.log(match);
/*
[
"12345", // matched substring
index: 14, // start position in str
input: "Order number: 12345",
groups: undefined
]
*/
String Methods Using Regex
- match(): Returns matched substrings as an array or
null. - matchAll(): Provides an iterator over all matches with detailed info (requires global
gflag). - replace(): Substitutes matched parts with a replacement string or function result.
- search(): Finds the first match index or
-1if none. - split(): Splits the string into an array using regex delimiter.
📌 Deep Dive: Using replace() and matchAll()
const text = "Email: user@example.com, Backup: admin@test.org";
// Replace emails with [hidden]
const censored = text.replace(/\b[\w.-]+@[\w.-]+\.\w+\b/g, "[hidden]");
console.log(censored);
// "Email: [hidden], Backup: [hidden]"
// Extract all emails using matchAll
const emailRegex = /\b[\w.-]+@[\w.-]+\.\w+\b/g;
const matches = text.matchAll(emailRegex);
for (const match of matches) {
console.log(match[0], "found at index", match.index);
}
/* Output:
user@example.com found at index 7
admin@test.org found at index 29
*/
💡 Important
Use the global g flag when you want to find all matches with matchAll() or replace(). Without g, only the first match is processed.
⚠️ Beware of exec() with Global Flag
When using exec() with a regex that has the global flag, it maintains a lastIndex property and successive calls may return different matches. Reset the regex or be cautious when looping.
| Method | Return Type | Typical Use |
|---|---|---|
test() | Boolean | Check presence of pattern |
exec() | Array or null | Detailed match info |
match() | Array or null | Get matched substrings |
matchAll() | Iterator | All matches with groups |
replace() | String | Modify matched text |
search() | Number (-1 if none) | Find first match position |
split() | Array | Divide string by pattern |
Quick Knowledge Check
Test what you just learned
Question 1 of 2
Which regex method returns a boolean indicating if a pattern exists in a string?
Question 2 of 2
What must you include in a regex to use matchAll() to find all matches?
Loading results...