Javascript Reference - JavaScript String search() Method








search() accepts a regular expression as the argument.

search() method returns the index of the first pattern occurrence in the string. search() returns -1 if not found.

Browser Support

search() Yes Yes Yes Yes Yes

Syntax

string.search(aValue);

Parameter Values

Parameter Description
aValue Required. A regular expression.




Return Value

Return the position of the first occurrence of the specified aValue.

Or -1 if no match is founds.

Example


var text = "loom, room, doom"; 
var pos = text.search(/asdf/); 
console.log(pos); //-1 

The code above generates the following result.