Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to return all matching strings against a regular expression in JavaScript ?

  • Last Updated : 06 Dec, 2021

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 JavaScript string.search() method. This method searches for a match between a regular expression in a given string.

Syntax:

let index = string.search( expression )

Parameters: The string.search() method accepts two parameters.

  • 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




<script>
  
    // Taking input a string.
    var string = "GeeksforGeeks is for computer science geeks";
  
  
    // Taking a regular expression.
    var regexp1 = /cie/;
    var regexp2 = /c/;
    var regexp3 = /z/;
  
    console.log(string.search(regexp1));
    // Expected Output: 31
  
    console.log(string.search(regexp2));
    // Expected Output: 21
  
    console.log(string.search(regexp3));
    // Expected Output: -1
</script>

Output:

31
21
-1

We 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




<script>
  
    // Taking an array of strings.
    let strings = [
        "GeeksforGeeks is computer science portal"
        "I am a Geek",
        "I am coder"
        "I am a student"
        "I am a computer science Geek"
    ];
  
    let i;
      
    // Taking a regular expression
    let regexp = /Gee/;
    let arr = [];
  
    for (i = 0; i < strings.length; i++) {
        if (strings[i].search(regexp) != -1) {
            arr.push(strings[i])
        }
    }
    console.log(arr)
</script>

Output:

['GeeksforGeeks is computer science portal', 'I am a Geek', 'I am computer science Geek']
0: "GeeksforGeeks is computer science portal"
1: "I am a Geek"
2: "I am a computer science Geek"
length: 3
[[Prototype]]: Array(0)

We successfully return an array of all the strings that match with a given regular expression.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!