1、exec() 方法是一个正则表达式方法,用于检索字符串中的正则表达式的匹配。
函数返回一个数组中,其中数组中存放匹配的结果;如果未找到匹配,则返回值为null。
2、test() 方法是一个正则表达式方法,用于检测一个字符串是否匹配某个模式,
如果字符串中含有匹配的文本,则返回true,否则返回false。
3、search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相配的子字符串,并返回子串的起始位置。
3.1、search() 方法使用正则表达式,使用正则表达式搜索字符串,且不区分大小写
3.2、search() 方法使用字符串, 可使用字符串作为参数。字符串参数会转换为正则表达式
4、replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子字符串。
4.1、replace() 方法使用正则表达式,使用正则表达式且不区分大小写将方法中的参数替换为字符串中的指定内容。
4.2、replace() 方法使用字符串,将接收字符串作为参数。
5、match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
代码实例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
<script>
var reg = new RegExp("e");
var str = "the best";
var str1 = "1de2de3";
//exec()方法
document.write(reg.exec(str) + "<br />");
//test()方法
document.write(reg.test(str) + "<br />");
//search()方法使用正则表达式
document.write(str.search(/best/i) + "<br />");
//search()方法使用字符串
document.write(str.search("best") + "<br />");
//replace()方法使用正则表达式
document.write(str.replace(/best/i,"foot") + "<br />");
//replace()方法使用字符串
document.write(str.replace("best","root") + "<br />");
//match()方法来检索一个字符串
document.write(str.match("est") + "<br />");
document.write(str.match("the") + "<br />");
document.write(str.match("bees") + "<br />");
//match()方法来检索一个正则表达式的匹配
document.write(str1.match(/\d+/g) + "<br />");
</script>
</html>