JavaScript search()Method

String 对象参考手册JavaScript String Object

Examples

Search for "Example":

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

nOutput:


Try it Yourself »

Definition and Usage

The search() method is used to search for a specified substring in a string, or to search for a substring that matches a regular expression.

If no matching substring is found, -1 is returned.

See more regular expression tutorialsRegExp Tutorial and our RegExp Object Reference.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the search() method


Syntax

string.search(searchvalue)

Parameter Values

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

Return Value

Type Description
Number The starting position of the String object that matches the specified string or regular expression.

Technical Details

JavaScript Version: 1.2


More Examples

Examples

Perform a case-sensitive search:

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

Output of the above example:


Try it Yourself »

Examples

Perform a case-insensitive search:

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

Output of the above example:


Try it Yourself »


String 对象参考手册JavaScript String Object

Other Extensions