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 match
  • m → 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:

  1. match()
  2. replace()
  3. split()
  4. 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 index 3.
  • 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

  1. Regex patterns are enclosed in / / slashes.
  2. Flags modify behavior (g, i, m).
  3. Character classes use brackets:

    • [a-z] → lowercase letters
    • [A-Z] → uppercase letters
    • [0-9] → digits
  4. Quantifiers:

    • + → one or more
    • * → zero or more
    • ? → zero or one
    • {n} → exactly n
  5. Capturing groups ( ) allow sub-matching and reuse.

🧩 Practice Review – Fill in the Gaps

  1. The .match() method is used to __ patterns in a string.
  2. [i-n]+ means match any letter from i to n, one or __ times.
  3. Capturing groups in regex are written inside __.
  4. The .replace() method replaces matched parts of a string with __.
  5. Using the g flag in regex allows .replace() to affect __ matches.
  6. .split() breaks a string into an __ of pieces wherever a pattern is found.
  7. The .search() method returns the __ where a match starts.
  8. If .search() can’t find a match, it returns __.
  9. In the example "string".match(/(r)[i-n]+/), the full match is "_____" and the captured group is "_____".
  10. Regular expressions are used in JavaScript to __, __, __, or __ parts of strings.

<
Previous Post
🧠 Input and Output Devices: The Post Office Analogy
>
Next Post
My Journey From School Labs to Celebrated Tech Trainer ✨