ES6 - search() 方法
此方法执行搜索,以查找正则表达式与此 String 对象之间的匹配项。
语法
string.search(regexp);
参数详细信息
regexp − 正则表达式对象。如果传递了非 RegExp 对象 obj,则使用 new RegExp(obj) 将其隐式转换为 RegExp。
返回值
如果成功,搜索将返回字符串内正则表达式的索引。否则,返回 -1。
示例
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" ); }
输出
Contains Apples.