Pattern Matching in JavaScript is performed using Regular Expressions (RegEx). It is used to search, match, replace, and validate strings based on predefined patterns.
Pattern matching is commonly used in:
- Email Validation
- Password Validation
- Mobile Number Validation
- Search Operations
- Replace Operations
- Form Validation
- Text Processing
/Pattern/OptionalIdentifiersvar regex = /pattern/flags;Example:
var regex = /hello/i;Here:
- hello → Pattern
- i → Identifier (Flag)
JavaScript provides several identifiers to modify the behavior of regular expressions.
| Identifier | Meaning |
|---|---|
| g | Global Search |
| i | Case Insensitive Search |
| m | Multi-line Search |
| s | Dot matches newline characters |
| u | Unicode matching |
| y | Sticky matching |
Searches for all occurrences of a pattern.
var str = "hello hello hello";
var result = str.match(/hello/g);
console.log(result);["hello","hello","hello"]
Ignores uppercase and lowercase differences.
var str = "HELLO";
console.log(/hello/i.test(str));true
Matches patterns across multiple lines.
var str = `Java
Script`;
console.log(/^Script/m.test(str));true
Allows dot (.) to match newline characters.
var str = "Hello\nWorld";
console.log(/Hello.World/s.test(str));true
Treats the pattern as Unicode characters.
var str = "😊";
console.log(/^.$/u.test(str));true
Matches only from the last matched position.
var regex = /hello/y;
regex.lastIndex = 0;
console.log(regex.test("hello world"));true
There are three important methods:
- search()
- replace()
- match()
The search() method searches for a pattern inside a string and returns the index position of the first match.
string.search(regex)Common.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Regular Expressions in JavaScript</title>
</head>
<body>
<p id="res"></p>
<script>
var str = "KodNest Technologies";
var n = str.search(/T/i);
document.getElementById("res").innerHTML = n;
</script>
</body>
</html>8
var str = "KodNest Technologies";
var n = str.search(/T/i);/T/searches for Tiignores uppercase/lowercase- search() returns:
8
because T is found at index 8.
The replace() method replaces the matched text with a new value.
string.replace(regex,newValue)<!DOCTYPE html>
<html lang="en">
<head>
<title>Regular Expressions in JavaScript</title>
</head>
<body>
<p id="res"></p>
<script>
var str = "KodNest Technologies";
var newStr = str.replace(/T/i,"X");
document.getElementById("res").innerHTML = newStr;
</script>
</body>
</html>KodNest Xechnologies
var newStr = str.replace(/T/i,"X");- Searches T
- Replaces T with X
Result:
KodNest Xechnologies
The match() method returns the matched value as an array.
string.match(regex)<!DOCTYPE html>
<html lang="en">
<head>
<title>Regular Expressions in JavaScript</title>
</head>
<body>
<p id="res"></p>
<script>
var str = "KodNest Technologies";
var result = str.match(/T/i);
document.getElementById("res").innerHTML = result;
</script>
</body>
</html>T
or
["T"]
The match() method returns:
["T"]because the character T is matched.
| Method | Purpose | Return Type |
|---|---|---|
| search() | Finds position | Index Number |
| replace() | Replaces matched text | New String |
| match() | Finds matched pattern | Array |
var str = "KodNest Technologies";
console.log(str.search(/T/i));
console.log(str.replace(/T/i,"X"));
console.log(str.match(/T/i));8
KodNest Xechnologies
["T"]
Pattern Matching is used in:
- Email Validation
- Password Validation
- Mobile Number Validation
- PAN Card Validation
- Aadhaar Number Validation
- Search Engines
- Text Editors
- Find and Replace Features
- Form Validation
- Data Extraction
Answer:
Pattern Matching is the process of searching and manipulating strings using Regular Expressions.
Answer:
/pattern/flagsExample:
/hello/iAnswer:
It performs case-insensitive matching.
Example:
/hello/iMatches:
hello
HELLO
Hello
Answer:
It returns the index position of the first matched character.
Answer:
It returns a new string after replacing the matched text.
Answer:
It returns an array containing the matched values.
Pattern Matching is one of the most important applications of Regular Expressions in JavaScript. By using identifiers (flags) and methods such as search(), replace(), and match(), developers can efficiently search, validate, replace, and manipulate strings. These concepts are widely used in real-world applications such as form validations, search engines, data extraction, and text processing systems.