How to return all matching strings against a regular expression in JavaScript ? Last Updated : 11 Aug, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to identify if a string matches with a regular expression and subsequently return all the matching strings in JavaScript. We can use the JavaScript string.search() method to search for a match between a regular expression in a given string. Syntax: let index = string.search( expression )Parameters: This method accepts two parameters that are given below: String name: The string name in which we want to search for a regular expression.Expression: It is the pattern/substring which we want to check if it is present in the above string.Return value: It returns the value of the index of the first matching regular expression in the given string else it returns -1. It starts from index 0, and if any alphabet is matched, it returns its corresponding index and does not check further. It returns the index of the first alphabet if any regular expression matches the corresponding string. Example 1: We can observe that the expression 'cie' is present at the 31st index of the given string. More precisely, it returns the index of the first alphabet of the first match regular expression, which in this case is 'c' in 'cie'. In the second case, it returns the index of the first 'c' alphabet whereas in the third case, it returns -1 since there is no expression 'z' in the given string. JavaScript // Taking input a string const str = "GeeksforGeeks is for computer science geeks"; // Taking a regular expression const regexp1 = /cie/; const regexp2 = /c/; const regexp3 = /z/; // Expected Output: 31 console.log(str.search(regexp1)); // Expected Output: 21 console.log(str.search(regexp2)); // Expected Output: -1 console.log(str.search(regexp3)); Output31 21 -1We can implement this method in an array of strings to selectively find all the matching strings against a regular expression. For this, we need to iterate all the given strings in the array and search for the regular expression in the string elements. Example 2: For implementing this, we will use the array push() method in JavaScript that adds an element in an array from the back. Syntax: array.push( element1, element2, element3, ... elementN )Example: JavaScript // Taking an array of str const str = [ "GeeksforGeeks is computer science portal", "I am a Geek", "I am coder", "I am a student", "I am a computer science Geek" ]; // Taking a regular expression const regexp = /Gee/; let arr = []; for (let i = 0; i < str.length; i++) { if (str[i].search(regexp) != -1) { arr.push(str[i]) } } console.log(arr) Output[ 'GeeksforGeeks is computer science portal', 'I am a Geek', 'I am a computer science Geek' ]We successfully return an array of all the strings that match with a given regular expression. Comment More infoAdvertise with us Next Article How to return all matching strings against a regular expression in JavaScript ? S souvikm02 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads JavaScript - How to Access Matched Groups in Regular Expression? Here are the different methods to access matched groups in JavaScript regular Expression(RegExp).1. Using exec() MethodThe exec() method returns an array with the entire match and captured groups, which you can access by their index in the result array.JavaScriptlet s = "The price is $50"; let regex 3 min read JavaScript - How to Use a Variable in Regular Expression? To dynamically create a regular expression (RegExp) in JavaScript using variables, you can use the RegExp constructor. Here are the various ways to use a variable in Regular Expression.1. Using the RegExp Constructor with a VariableIn JavaScript, regular expressions can be created dynamically using 3 min read How to clone a given regular expression in JavaScript ? In this article, we will know How to clone a regular expression using JavaScript. We can clone a given regular expression using the constructor RegExp(). The syntax of using this constructor has been defined as follows:- Syntax: new RegExp(regExp , flags) Here regExp is the expression to be cloned a 2 min read How to build a Math Expression Tokenizer using JavaScript ? A math expression tokenizer is a fundamental component in parsing mathematical expressions. It breaks down a mathematical expression into smaller units called tokens, which are easier to process and evaluate. In JavaScript, building a math expression tokenizer can be achieved through various approac 2 min read How to Search a String for a Pattern in JavaScript ? Here are two ways to search a string for a pattern in JavaScript.1. Using the string search() MethodThe JavaScript string search() method is used to search a specified substring within the given string and regular expression. It returns the index of the first occurrence of the pattern or -1 if the p 2 min read Convert user input string into regular expression using JavaScript In this article, we will convert the user input string into a regular expression using JavaScript.To convert user input into a regular expression in JavaScript, you can use the RegExp constructor. The RegExp constructor takes a string as its argument and converts it into a regular expression object 2 min read How to get nth occurrence of a string in JavaScript ? In this article, the task is to get the nth occurrence of a substring in a string with the help of JavaScript. We have many methods to do this some of which are described below:Approaches to get the nth occurrence of a string:Table of Content Using split() and join() methodsUsing indexOf() methodUsi 5 min read How to Replace All Occurrences of a String in JavaScript? Here are different approaches to replace all occurrences of a string in JavaScript.1. Using string.replace() MethodThe string.replace() method is used to replace a part of the given string with another string or a regular expression. The original string will remain unchanged.Syntaxstr.replace(replac 2 min read How to replace a portion of strings with another value in JavaScript ? In JavaScript, replacing a portion of strings with another value involves using the `replace()` method or regular expressions. This process is essential for text manipulation tasks like formatting, sanitization, or dynamic content generation in web development and data processing applications. We ca 3 min read JavaScript - How to Create Regular Expression Only Accept Special Formula? To ensure that a string matches a specific formula or pattern, you can use a regular expression (RegExp) in JavaScript. Here are the various ways to create a regular expression that only accepts regular formulas.1: Alphanumeric Code FormulaLet's create a regular expression that matches a formula lik 3 min read Like