目录

search()

此方法执行正则表达式与此String对象之间的匹配搜索。

语法 (Syntax)

string.search(regexp);

参数细节 (Argument Details)

  • regexp - 正则表达式对象。 如果传递了非RegExp对象obj,则使用新的RegExp(obj)将其隐式转换为RegExp。

返回值 (Return Value)

如果成功,搜索将返回字符串中正则表达式的索引。 否则,它返回-1。

例子 (Example)

var re = /apples/gi;
var str = "Apples are round, and apples are juicy."; 
if ( str.search(re) == -1 ) { 
   console.log("Does not contain Apples" ); 
} else { 
   console.log("Contains Apples" ); 
}   

输出 (Output)

Contains Apples.    
↑回到顶部↑
WIKI教程 @2018