HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

JavaScript String search() Method

String Object Reference JavaScript String Object

Example

Search for "W3Schools":

var str = "Visit W3Schools!";
var n = str.search("W3Schools");

The result of n will be:


Try it yourself »

Definition and Usage

The search() method searches a string for a specified value, or regular expression, and returns the position of the match.

This method returns -1 if no match is found.

Read more about regular expressions in our RegExp Tutorial and our RegExp Object Reference.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The search() method is supported in all major browsers.


Syntax

string.search(searchvalue)

Parameter Values

Parameter Description
searchvalue Required. The value, or regular expression, to search for.

Return Value

Type Description
Number The position of the first occurance of the specified searchvalue, or -1 if no match is found

Technical Details

JavaScript Version: 1.2


More Examples

Example

Perform a case-sensitive search:

var str = "Mr. Blue has a blue house";
var n = str.search("blue");

The result of n will be:


Try it yourself »

Example

Perform a case-insensitive search:

var str = "Mr. Blue has a blue house";
var n = str.search(/blue/i);;

The result of n will be:


Try it yourself »


String Object Reference JavaScript String Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE