温馨提示×

javascript正则函数都有哪些

养鱼的猫咪
176
2021-04-12 13:17:39
栏目: 编程语言

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

0