search方法

转自:

http://www.fzs8.net/Java/JavaScript/2007-05-25/4093.html

定义与用法

The search() method is used to search a string for a specified value.
search()方法用于从字符串中寻找指定值的位置

Syntax
语法

stringObject.search(searchstring)

Parameter
参数
Description
注释
searchstring
所寻找的字符串
Required. The value to search for in a string. To perform a case-insensitive search add an 'i' flag
必选项。所要寻找匹配的字符串。要进行模糊匹配添加一个”i“标记


Tips and Notes
注意

Note: search() is case sensitive.
注意:search()是一个精确匹配

Note: The search() method returns the position of the specified value in the string. If no match was found it returns -1.
注意:search()方法返回的是指定字符串在字符串中的位置,如果没有匹配字符串则返回 -1


Example 1 - Standard Search
实例1 - 标准查找

In the following example we will search for the word "W3Schools":
在下面的例中,我们将查找”test“:

<script type="text/javascript">
var str="欢迎来到test"
document.write(str.search(/test/))
</script>

The output of the code above will be:
输出结果为:

4

Note: In the following example the word "w3schools" will not be found (because the search() method is case sensitive):
注意:在下面的例子中将无法找到"test"(search()方法是精确匹配的)

<script type="text/javascript">
var str="欢迎来到test"
document.write(str.search(/W3pop/))
</script>

The output of the code above will be:
返回结果为:

-1


Example 2 - Case-insensitive Search
实例 2 - 模糊查找

In the following example we will perform a case-insensitive search:
在下面的例子中,我们将演示一个模糊查找:

<script type="text/javascript">
var str="欢迎来到test"
document.write(str.search(/W3pop/i))
</script>

The output of the code above will be:
返回的结果为:

4

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值