Open In App

How to search a string for a pattern in JavaScript ?

In this article, we will see how to search a string for a pattern in JavaScript. We will be using the following methods to search a string:

Approach 1: Here, we will learn how to search for a string that includes a given pattern in JavaScript. The string.search() method is the inbuilt method in JavaScript that is used for this purpose. It searches for a match in between regular expressions in a given String. 



Syntax:

let position = str.search( expression )

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



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.

Example 1:




<script>
  
    // Taking input a string.
    var string = "GeeksforGeeks is computer science portal";
    
    // Taking a regular expression.
    var regexp1 = /G/;  
    var regexp2 = /c/;
    var regexp3 = /z/;
    
    // Output
    console.log(string.search(regexp1));
    //Expected Output: 0
    
    console.log(string.search(regexp2));
    // Expected Output: 17
    
    console.log(string.search(regexp3));
    // Expected Output: -1
</script>

Output:

0
17
-1

Explanation: We can observe the first ‘G‘ match occurs at index 0, whereas the first match of ‘c‘ occurs at the 17th index whereas the alphabet ‘z‘ is not present in the String “GeeksforGeeks is computer science portal”, thus returns -1.

Example 2:




<script>
  
    // Taking input a string.
    var string = "GeeksforGeeks is computer science portal";
          
    // Taking a regular expression.
    var regexp = /cie/;  
  
    console.log(string.search(regexp));
    // Expected Output: 27
</script>

Output:

27

Explanation: We can observe that the expression ‘cie’ matches with the String at index 27. Thus, it returns the index of the first matching element of the regular expression ( here ‘c’ in ‘cie’ ) if it is present in the given string.

Approach 2: We can also use the Javascript String match() function, which returns an array that contains the given expression whenever a match is found with the given String else returns null.

Syntax:

string.match( expression )

Parameters: Here, it takes two parameters:

Example 1:




<script>
  
    // Taking input a string.
    let string = "GeeksforGeeks is computer science portal";
    console.log(string.match(/rGe/g));    
</script>

Output:

['rGe']
0: "rGe"
length: 1
[[Prototype]]: Array(0)

Thus, it returns an array whose length is one, since there is only one match between the given expression and the string. The “g” flag helps to find case sensitive match between the given string and expression.

For a global, case-insensitive match, we can use “gi” flag that will return all possible combinations from the given string.

Example 2:




<script>
  
    // Taking input a string.
    let string = "GeeksforGeeks is computer "
        + "science portal for computer geeks";
        console.log(string.match(/gee/gi));
</script>

Output:

['Gee', 'Gee', 'gee']
0: "Gee"
1: "Gee"
2: "gee"
length: 3
[[Prototype]]: Array(0)

Thus, it returns an array of length three, that contains all possible combinations between the given expression/pattern and the string.


Article Tags :