Tutorials Tonight's Logo
Follow Us

JavaScipt String Search Using regex With search() Method


In this tutorial, you will learn how to search whether a keyword, substring, character or certain pattern exist 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 approach and will compare different approaches with the search method.

JavaScipt 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

let string = "To do or not to do.";
let index = string.search(/not/); // return 9
console.log(index);

// character at index
console.log(string.charAt(index));
▶ Try It

The above example contains a string in which it searches for a pattern (/not/), the pattern is found at index 9 using the search() method. Now we can use the index to get the character at that position using the charAt method.


Syntax of search method in string

The syntax of search() method is as following:

string.search(regex);
  1. string - The method calling string.

  2. 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 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 afterwards 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));
▶ Try It

# 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));
▶ Try It

# 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);
▶ Try It

# 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);
▶ Try It

# 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));
▶ Try It

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 indexOf and search methods are as follows:

indexOf Method search Method
indexOf method use string to find a match in string search method use regular expression to find a match in string
indexOf method is faster compare to search method search method is slower as compare to indexOf method
In the indexOf method, you can have starting search position as the second argument. Example str.indexOf('book', 10) search method doesn't provide starting search position

Points to remember:

  1. The indexOf() returns the index of the first occurrence of a string within another string if no such occurrence is found then returns -1.
  2. The indexOf method is case-sensitive.
  3. Using the indexOf method you can find both string and character within a string.

More Learning

Want to learn more about programming and web development? We have a few recommendations for you.

Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.