当前位置:首页 > 千层云服务器 > 正文

javascript正则函数都有哪些

javascript中的正则函数有:1.test,检测一个字符串是否匹配某个模式;2.match,在字符串内检索指定的值;3.replace,在字符串中替换字符;4.search,检索字符串中指定的子字符串;

javascript正则函数都有哪些

javascript中的正则函数有以下几种

1.test

javascript中test函数的作用是用于检测一个字符串是否匹配某个模式。

test函数使用方法:

var str = "hello world!";

var patt1 = new RegExp("world");

var result = patt1.test(str);

document.write(result); //返回true

2.match

javascript中match函数的作用是用于在字符串内检索指定的值。

match函数使用方法:

var str="Hello world!"

document.write(str.match("world!")) //返回world!

document.write(str.match("worlld")) //返回null

3.replace

javascript中replace函数的作用是用于在字符串中替换字符。

replace函数使用方法:

var str="Hello world!"

document.write(str.replace(/world!/, "China!")) //返回Hello China!

4.search

javascript中search函数的作用是用于检索字符串中指定的子字符串。

search函数使用方法:

var str="Hello world!"

document.write(str.search(/world!/)) //返回6

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

在线客服