JavaScript String search() Method
In this tutorial, you will learn how to search whether a keyword, substring, character, or certain pattern exists in a string or not, if exist then get its position. We will mainly focus on the search method but will also look at other approaches and will compare different approaches with the search method.
JavaScript String Search
If you want to know whether a word or a pattern can be found within a string and also want to know its position (index value), then use the string search() method in JavaScript.
The search() method uses a regular expression to search for a match within the string.
If there is a match with the regular expression then it returns the index of the first match, if there is no match then it returns -1.
Example
var string = "To do or not to do.";
// search "do" in the string
var index = string.search(/do/);
console.log(index); // output: 3
In the above example, we have used the regular expression to search for the word "do" in the string "To do or not to do." and it returns the first index value which is 3. You can see we also have another match at index value 16.
Syntax of search method in string
The syntax of search() method is as following:
str.search(regex);
-
str is the string that you want to search for the regex pattern.
-
regex - The regular expression that is used to search for a match in the string. If a non-RegExp object is passed then it is implicitly converted to regex using the new RegExp() method.
String search example
Let us look at some examples of searching for a pattern within a string.
# Example 1: Using regex
The following example uses a regular expression to search and get the index of a word that is separated with a dash and space afterward in the given string.
Example
let string = "Keep work track in to-do list.";
let index = string.search(/\w+-\w+\s/); // return 19
console.log(index);
// character at index
console.log(string.charAt(index));
# Example 2: Using string as RegExp
In this example, we have passed a string in the search() method in the place of regex. The method implicitly converts it to a regular expression.
Example
let string = "To do or not to do.";
let index = string.search("do"); // return 3
console.log(index);
// character at index
console.log(string.charAt(index));
# Example 3: No argument passed
If no argument or an empty string ('') is passed in the search method then it returns 0.
Example
let string = "To do or not to do.";
// returns 0
let index = string.search();
/* or
let index = string.search('');
*/
console.log(index);
# Example 4: No Match Found
In case there is no match found in the search() method, it returns -1.
Example
let string = "To do or not to do.";
// returns -1
let index = string.search(/go/);
console.log(index);
# Example 5: Case-insensitive search
To search in the string case-insensitively use the /i flag with the regular expression.
Example
let string = "To do or not to do.";
// case insensitive
let index = string.search(/Do/i);
console.log(index);
// character at index
console.log(string.charAt(index));
Difference between search() and indexOf() in javascript
You must be aware of the indexOf string method. It also finds a match from a string and returns its index.
The difference between search and indexOf methods are as follows:
search Method | indexOf Method |
---|---|
search method uses a regular expression to find a match in the string | indexOf method uses a string to find a match in the string |
The search method is slower compared to the indexOf method | indexOf method is faster compared to the search method |
search method doesn't provide starting search position | In the indexOf method, you can have starting search position as the second argument. Example str.indexOf('book', 10) |
Conclusion
The search() method is used when you want to search for a pattern within a given string using regular expressions. Once it finds a match it will return the index of the first match. If no such match is found then it returns -1.