当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JavaScript search() 方法

JavaScript
JS数学对象参考
JS字符串对象参考
JS函数参考
JS事件参考
Javascript 常用正则表达式
FF和IE下的js兼容性问题
jQuery 简单介绍
jQuery / 核心 / $(expression, [context] ) 函数
jQuery / 核心 / $(html) 函数
如何使用JS来判断浏览器类型(ie、firefox,等等)
Javascript在IE和FireFox中的不同表现
3个js字符编码函数区别
javascript 中的 XMLDOM 对象

JavaScript search() 方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 407 ::
收藏到网摘: n/a

Definition and Usage
定义与用法

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":
在下面的例中,我们将查找”RuanChen“:

<script type="text/javascript">
var str="欢迎来到RuanChen"
document.write(str.search(/RuanChen/))
</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):
注意:在下面的例子中将无法找到"RuanChen"(search()方法是精确匹配的)

<script type="text/javascript">
var str="欢迎来到RuanChen"
document.write(str.search(/RuanChen/))
</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="欢迎来到RuanChen"
document.write(str.search(/RuanChen/i))
</script>

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

4


Try-It-Yourself Demos
互动演练

search()
How to use search() to search a string for a specified value.
如何用search()来查找一个指定的值

search() Case-insensitive search
How to use search() to search a string for a specified value.
如何用search()进行模糊查找

评论 (0) All

登陆 | 还没注册?