
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to return all matching strings against a RegEx in JavaScript?
In this article, we will explore the essentials of a regular expression (RegEx) and how to compare other strings with this regex in JavaScript. We will learn how to identify a similar string with that of a regex and then subsequently return them. We can use the string.search() method to match the regular expression in a given string.
Syntax
let index = string.search(expression)
Parameters
string − This is the string that will be searched in comparison with the expression.
expression − This is the regular expression that will be used for checking with the original string.
Example 1
In the below example, we are going to compare a string with a substring/regular expression that is rendered in the function. This method will only return the elements that match the expression, else the string will not be returned.
# index.html
<!DOCTYPE html> <html> <head> <title> Comparing Strings </title> </head> <body> <h2 style="color:green"> Welcome To Tutorials Point </h2> </body> <script> // Taking a String input. var string = "Start Learning new technologies now."; // Defining a regular expression var regexp1 = /Lea/; var regexp2 = /rn/; var regexp3 = /abc/; console.log("Matched at:"+string.search(regexp1)); // Expected Output: 6 console.log("Matched at:"+string.search(regexp2)); // Expected Output: 9 console.log("Matched at:"+string.search(regexp3)); // Expected Output: -1 </script> </html>
Output
Example 2
# index.html
<!DOCTYPE html> <html> <head> <title> Comparing Strings </title> </head> <body> <h2 style="color:green"> Welcome To Tutorials Point </h2> </body> <script> // Taking input an array of strings. 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 i; // Defining the regular expression let regexp = /Tutorials/; let result = []; for (i = 0; i < input.length; i++) { if (input[i].search(regexp) != -1) { result.push(input[i]) } } console.log("Resultant array is:"+result) </script> </html>
Output
- Related Articles
- Matching strings for similar characters - JavaScript
- JavaScript Return an array that contains all the strings appearing in all the subarrays
- Pattern matching in C# with Regex
- Pattern matching in Python with Regex
- Matching strings with a wildcard in C#
- How to check multiple regex patterns against an input? Using Java.
- How to compare two strings using regex in Python?
- Matching Nonprintable Characters using Java regex
- JavaScript filter an array of strings, matching case insensitive substring?
- How to return static strings in MySQL?
- Forming and matching strings of an array based on a random string in JavaScript
- Function to check two strings and return common words in JavaScript
- Compare Strings in JavaScript and return percentage of likeliness
- How to Match patterns and strings using the RegEx module in Python
- Regex to match lines containing multiple strings in Java
