JS REGEX 07: Mastering JavaScript’s String Methods with RegExp

Regular Expressions, often called regex or regexp, are powerful tools for pattern matching within strings. They are used to search, extract, replace, or split parts of text based on specific patterns rather than fixed words.
In JavaScript, regex patterns are typically written between forward slashes (/pattern/), and can include flags such as:
g→ global match (find all occurrences)i→ case-insensitive matchm→ multiline match
For example:
/a-z/ // matches lowercase letters a through z
/[0-9]+/ // matches one or more digits
/cat/gi // matches "cat", "Cat", "CAT", etc., globally
🔍 The Four Core Regex Methods in JavaScript
JavaScript provides four main string methods that work directly with regex patterns:
match()replace()split()search()
Let’s break each one down.
1️⃣ match() — Find Patterns in a String
The match() method is used to find and return parts of a string that match a regex pattern.
Syntax:
string.match(pattern)
Example:
console.log("string".match(/[i-n]+/));
// Output: ["in"]
Explanation:
[i-n]→ match any letter from i to n+→ one or more occurrences"in"matches this pattern inside"string"
Capturing Groups
Parentheses ( ) create capturing groups, which remember specific parts of a match.
console.log("string".match(/(r)[i-n]+/));
// Output: ["rin", "r"]
"rin"is the entire match"r"is the captured group
Capturing groups are useful when you need to extract or reference certain portions of a matched string.
2️⃣ replace() — Replace Matched Patterns
The replace() method substitutes matched patterns with new content.
Syntax:
string.replace(pattern, replacement)
Example:
console.log("string".replace(/[i-n]+/, "foo"));
// Output: "strfoog"
Explanation:
- The pattern
[i-n]+finds"in" - It’s replaced with
"foo", resulting in"strfoog"
To replace all matches, include the global flag /g:
console.log("stringstring".replace(/[i-n]+/g, "X"));
// Output: "strXgstrXg"
replace() can also use functions as replacements to perform dynamic changes based on each match.
3️⃣ split() — Split String by a Pattern
The split() method divides a string into an array of substrings, using a regex as the delimiter.
Syntax:
string.split(pattern)
Example:
console.log("stringstring".split(/[i-n]+/));
// Output: ["str", "gstr", "g"]
Explanation:
- Each
"in"occurrence is used as a separator. - The string gets split into three parts:
["str", "gstr", "g"]
split() is especially useful for tokenizing text, e.g., separating by spaces, commas, or custom patterns.
4️⃣ search() — Find Index of a Match
The search() method looks for a pattern and returns the index of its first occurrence.
If no match is found, it returns -1.
Syntax:
string.search(pattern)
Example:
console.log("string".search(/[i-n]+/)); // Output: 3
console.log("string".search(/[o-q]+/)); // Output: -1
Explanation:
- The first search finds
"in"starting at index3. - The second search finds no match, so it returns
-1.
📋 Summary Table
| Method | Description | Returns |
|---|---|---|
match() |
Finds and returns content that matches the regex | Array of matches |
replace() |
Replaces matched content with new text | New string after replacement |
split() |
Splits string by the matched pattern | Array of substrings |
search() |
Finds index of the first match or -1 | Number (index position) |
🧠 Key Concepts to Remember
- Regex patterns are enclosed in
/ /slashes. - Flags modify behavior (
g,i,m). -
Character classes use brackets:
[a-z]→ lowercase letters[A-Z]→ uppercase letters[0-9]→ digits
-
Quantifiers:
+→ one or more*→ zero or more?→ zero or one{n}→ exactly n
- Capturing groups
( )allow sub-matching and reuse.
🧩 Practice Review – Fill in the Gaps
- The
.match()method is used to __ patterns in a string. [i-n]+means match any letter from i to n, one or __ times.- Capturing groups in regex are written inside __.
- The
.replace()method replaces matched parts of a string with __. - Using the
gflag in regex allows.replace()to affect __ matches. .split()breaks a string into an __ of pieces wherever a pattern is found.- The
.search()method returns the __ where a match starts. - If
.search()can’t find a match, it returns __. - In the example
"string".match(/(r)[i-n]+/), the full match is"_____"and the captured group is"_____". - Regular expressions are used in JavaScript to __, __, __, or __ parts of strings.