Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to return all matching strings against a RegEx in JavaScript?
In JavaScript, regular expressions (RegEx) are powerful tools for pattern matching in strings. While string.search() returns the position of the first match, there are multiple methods to return all matching strings or substrings against a RegEx pattern.
Using string.search() Method
The search() method returns the index of the first match, or -1 if no match is found.
Syntax
let index = string.search(expression)
Parameters
string ? The string to be searched
expression ? The regular expression pattern to match
Example
<!DOCTYPE html>
<html>
<head>
<title>String Search Example</title>
</head>
<body>
<h2 style="color:green">Welcome To Tutorials Point</h2>
<script>
var string = "Start Learning new technologies now.";
var regexp1 = /Lea/;
var regexp2 = /rn/;
var regexp3 = /abc/;
console.log("Matched 'Lea' at: " + string.search(regexp1));
console.log("Matched 'rn' at: " + string.search(regexp2));
console.log("Matched 'abc' at: " + string.search(regexp3));
</script>
</body>
</html>
Matched 'Lea' at: 6 Matched 'rn' at: 9 Matched 'abc' at: -1
Finding All Matching Strings in an Array
To return all strings from an array that match a RegEx pattern, use array filtering with search():
<!DOCTYPE html>
<html>
<head>
<title>Filter Matching Strings</title>
</head>
<body>
<h2 style="color:green">Welcome To Tutorials Point</h2>
<script>
let input = [
"Tutorials Point has 40 million readers every month",
"Start your journey with Tutorials Point",
"It started in 2006",
"Start Learning",
"Tutorials Point started in 2006"
];
let regexp = /Tutorials/;
let result = [];
for (let i = 0; i < input.length; i++) {
if (input[i].search(regexp) !== -1) {
result.push(input[i]);
}
}
console.log("Matching strings:");
result.forEach(str => console.log("- " + str));
</script>
</body>
</html>
Matching strings: - Tutorials Point has 40 million readers every month - Start your journey with Tutorials Point - Tutorials Point started in 2006
Using match() Method for All Occurrences
The match() method with global flag returns all matches within a string:
<!DOCTYPE html>
<html>
<head>
<title>Match All Occurrences</title>
</head>
<body>
<h2 style="color:green">Welcome To Tutorials Point</h2>
<script>
let text = "The cat in the hat sat on the mat";
let pattern = /at/g; // Global flag finds all matches
let matches = text.match(pattern);
console.log("All 'at' matches:", matches);
// Using word boundaries for whole words
let wordPattern = /\bcat\b/g;
let wordMatches = text.match(wordPattern);
console.log("Whole word 'cat' matches:", wordMatches);
</script>
</body>
</html>
All 'at' matches: ["at", "at", "at", "at"] Whole word 'cat' matches: ["cat"]
Using filter() with Modern JavaScript
A cleaner approach using array methods:
<!DOCTYPE html>
<html>
<head>
<title>Filter with Modern JS</title>
</head>
<body>
<h2 style="color:green">Welcome To Tutorials Point</h2>
<script>
let strings = [
"JavaScript is awesome",
"Python is great",
"Java programming",
"JavaScript frameworks",
"C++ development"
];
// Filter strings containing "Java"
let javaStrings = strings.filter(str => /Java/.test(str));
console.log("Strings containing 'Java':");
javaStrings.forEach(str => console.log("- " + str));
// Case-insensitive search
let scriptStrings = strings.filter(str => /script/i.test(str));
console.log("\nStrings containing 'script' (case-insensitive):");
scriptStrings.forEach(str => console.log("- " + str));
</script>
</body>
</html>
Strings containing 'Java': - JavaScript is awesome - Java programming - JavaScript frameworks Strings containing 'script' (case-insensitive): - JavaScript is awesome - JavaScript frameworks
Comparison of Methods
| Method | Returns | Use Case |
|---|---|---|
string.search() |
Index of first match | Check if pattern exists |
string.match() |
Array of matches | Extract all matches from string |
array.filter() |
Filtered array | Get matching strings from array |
Conclusion
Use string.search() to find if a pattern exists, match() with global flag for all matches within a string, and filter() to return all matching strings from an array. Choose the method based on your specific requirement.
