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

JavaScript String search() Method

JavaScript String Reference JavaScript String Reference

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, and returns the position of the match.

The search value can be string or a regular expression.

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. A regular expression.
A string will automatically be converted to a regular expression.

Return Value

Type Description
Number The position of the first occurrence 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 »


JavaScript String Reference JavaScript String Reference

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 FREE Website Templates Free HTML Templates

SHARE THIS PAGE

facebook