当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 简介JavaScript中search()方法的使用

简介JavaScript中search()方法的使用

2019年08月03日  | 移动技术网IT编程  | 我要评论
 此方法执行搜索正则表达式,string对象之间的匹配。 语法 string.search(regexp); 下面是参数的详细信息:

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

string.search(regexp);

下面是参数的详细信息:

  •     regexp : 正则表达式对象。如果非regexp对象obj传递,它是隐式通过使用new regexp(obj) 转换为regexp

返回值:

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

例子:

<html>
<head>
<title>javascript string search() method</title>
</head>
<body>
<script type="text/javascript">

var re = /apples/gi;
var str = "apples are round, and apples are juicy.";

if ( str.search(re) == -1 ){
  document.write("does not contain apples" );
}else{
  document.write("contains apples" );
}

</script>
</body>
</html>

这将产生以下结果:

contains apples

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网