简介JavaScript中search()方法的使用
此方法执行搜索正则表达式,String对象之间的匹配。
语法
string.search(regexp);
下面是参数的详细信息:
- regexp:正则表达式对象。如果非RegExp对象obj传递,它是隐式通过使用newRegExp(obj)转换为RegExp
返回值:
- 如果成功的话,搜索将返回字符串内的正则表达式的索引。否则,它返回-1.
例子:
<html>
<head>
<title>JavaScriptStringsearch()Method</title>
</head>
<body>
<scripttype="text/javascript">
varre=/apples/gi;
varstr="Applesareround,andapplesarejuicy.";
if(str.search(re)==-1){
document.write("DoesnotcontainApples");
}else{
document.write("ContainsApples");
}
</script>
</body>
</html>
这将产生以下结果:
ContainsApples