【www.gdgbn.com--网页配色】

语法
stringobject.match(searchvalue)
stringobject.match(regexp)

<script type="text/网页特效">
var str="hello world!"
document.write(str.match("world") + "
") //world
document.write(str.match("world") + "
") //null
document.write(str.match("worlld") + "
") //null
document.write(str.match("world!")) //world!
</script>


下面的示例演示了js中match函数方法的用法:

function matchdemo(){
   var r, re;         // 声明变量。
   var s = "the rain in spain falls mainly in the plain";
   re = /ain/i;    // 创建正则表达式模式。
   r = s.match(re);   // 尝试匹配搜索字符串。
   return(r);         // 返回第一次出现 "ain" 的地方。
}

本示例说明带 g 标志设置的js中match函数方法的用法

function matchdemo(){
   var r, re;         // 声明变量。
   var s = "the rain in spain falls mainly in the plain";
   re = /ain/ig;      // 创建正则表达式模式。
   r = s.match(re);   // 尝试去匹配搜索字符串。
   return(r);         // 返回的数组包含了所有 "ain"
                      // 出现的四个匹配。
}


注意:在全局检索模式下,match() 即不提供与子表达式匹配的文本的信息,也不声明每个匹配子串的位置。如果您需要这些全局检索的信息,可以使用 regexp.exec()。

本文来源:http://www.gdgbn.com/wangyezhizuo/28264/