Common Regex Methods

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.

Illustration of Common Regex Methods
Illustration of Common Regex Methods
Common Regex Methods Overview
MethodTypeDescription
test()RegexTests if a pattern exists in a string; returns true or false.
exec()RegexReturns detailed match info as an array or null if no match.
match()StringReturns match info from a string using a regex.
matchAll()StringReturns an iterator for all matches with detailed info.
replace()StringReplaces matched substrings using a regex pattern.
search()StringReturns the index of the first match or -1 if none.
split()StringSplits 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()

JAVASCRIPT
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 g flag).
  • replace(): Substitutes matched parts with a replacement string or function result.
  • search(): Finds the first match index or -1 if none.
  • split(): Splits the string into an array using regex delimiter.

📌 Deep Dive: Using replace() and matchAll()

JAVASCRIPT
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.

Regex Methods: Return Types Comparison
MethodReturn TypeTypical Use
test()BooleanCheck presence of pattern
exec()Array or nullDetailed match info
match()Array or nullGet matched substrings
matchAll()IteratorAll matches with groups
replace()StringModify matched text
search()Number (-1 if none)Find first match position
split()ArrayDivide string by pattern