JavaScript String search() Method

Definition and Usage

search() The method matches a string with a regular expression.

Note:If the search value is a string, it is converted to a regular expression.

search() The method returns the index (position) of the first matching item.

If no match is found, then search() The method returns -1.

Tip:search() The method distinguishes between uppercase and lowercase.

See also:

Regular Expression Tutorial

Regular Expression Reference Manual

The difference between String search() and String indexOf()

search() Cannot use the starting position parameter.

indexOf() The method cannot search regular expressions.

The difference between String search() and String match()

search() The method returns the position of the first match.

match() The method returns an array of matches.

Instance

Example 1

Search "Blue":

let text = "Mr. Blue has a blue house";
let position = text.search("Blue");

Try It Yourself

Example 2

Search "blue":

let text = "Mr. Blue has a blue house";
let position = text.search("blue");

Try It Yourself

Example 3

Search /Blue/:

let text = "Mr. Blue has a blue house";
let position = text.search(/Blue/);

Try It Yourself

Example 4

search /blue/:

let text = "Mr. Blue has a blue house";
let position = text.search(/blue/);

Try It Yourself

Example 5

Case-insensitive Search:

let text = "Mr. Blue has a blue house";
let position = text.search(/blue/i);

Try It Yourself

Syntax

string.search(regexp)

parameter

parameter Description
regexp

Required. Search value.

Regular Expression (or a string that can be converted to a Regular Expression).

Return Value

Type Description
number the first match position. If it does not match, it returns -1.

Technical Details

parameter regexp

The parameter is the string string is the RegExp object searched in the string, which has the specified pattern. If the parameter is not a RegExp object, it is first passed to the RegExp() constructor, and it is converted into a RegExp object.

Return Value

string the first occurrence of regexp the starting position of the matching substring.

Note:If no matching substring is found, it returns -1.

Description

search() method searches for string to search for regexp matching substrings, and returns the position of the first character of the first matching substring. If no matching substring is found, it returns -1.

search() method does not perform global matching, it will ignore the flag g. It also ignores regexp the lastIndex property, and it always starts searching from the beginning of the string, which means it always returns string the first match position.

Regular Expression Search Methods

In JavaScript, regular expression text search can be completed with different methods.

These are the most commonly used methods by using the pattern as a regular expression:

Example Description
text.match(pattern) String Method match()
text.search(pattern) String Method search()
pattern.exec(text) Regular Expression Method exec()
pattern.test(text) Regular Expression Method test()

Browser Support

search() is an ECMAScript1 (ES1) feature.

All browsers fully support ES1 (JavaScript 1997):

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support

Related Pages

JavaScript String

JavaScript String Methods

JavaScript String Search