Latest web development tutorials

JavaScript search () method

String Object Reference JavaScript String Object

Examples

Find "W3CSchool":

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

n output:

var str="Visit W3Schools!" document.write(str.search("W3CSchool"));

try it"

Definition and Usage

search () method is used to retrieve the string specified in substring, or retrieve the regular expression matches the substring.

If you do not find any matching substring, -1 is returned.

See more regular expressions tutorial RegExp tutorials and Our RegExp Object Reference .


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support search () method


grammar

string.search( searchvalue )

Parameter Value

参数 描述
searchvalue 必须。查找的字符串或者正则表达式。

return value

类型 描述
Number 与指定查找的字符串或者正则表达式相匹配的 String 对象起始位置。

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"));

Examples of the above output:


try it"

Examples

Perform a case-insensitive Search:

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

Examples of the above output:


try it"


String Object Reference JavaScript String Object