How to search a string for a pattern in JavaScript ?
Last Updated :
28 Nov, 2021
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:
- String name: The name of the String in which we want to search a pattern is taken as a parameter.
- 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.
Example 1:
JavaScript
<script>
var string = "GeeksforGeeks is computer science portal" ;
var regexp1 = /G/;
var regexp2 = /c/;
var regexp3 = /z/;
console.log(string.search(regexp1));
console.log(string.search(regexp2));
console.log(string.search(regexp3));
</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:
JavaScript
<script>
var string = "GeeksforGeeks is computer science portal" ;
var regexp = /cie/;
console.log(string.search(regexp));
</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:
- String name: The name of the String in which we want to search a pattern is taken as a parameter.
- Expression: It is the pattern/ substring which we want to check if it is present in the above String.
Example 1:
JavaScript
<script>
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:
JavaScript
<script>
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...