Home » JavaScript String search() Method

JavaScript String search() Method

by Online Tutorials Library

JavaScript String search() Method

The JavaScript string search() method is used to search the regular expression in the given string. This method returns -1, if match is not found.

Syntax

The search() method is represented by the following syntax:

Parameter

regexp – It represents the regular expression which is to be searched.

Return

The position of searched character.

JavaScript String search() Method Example

Let’s see some examples of search() method.

Example 1

Let’s see a simple example to search for a string.

Test it Now

Output:

16  

Example 2

In this example, we will see that search() method is case-sensitive.

Test it Now

Output:

36  

Example 3

We can ignore case-sensitive behaviour of search() method by using ignore flag. Let’s understand with the help of example:

Test it Now

Output:

16  

Example 4

In this example, we will search a regular expression which is not present in the given string.

Test it Now

Output:

-1  

Next TopicJavaScript String

You may also like