JavaScript test() 方法

html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<script>
var str="Hello world!";
//查找"Hello"
var patt=/Hello/g;
var result=patt.test(str);
document.write("返回值: " +  result); 
//查找 "Runoob"
patt=/Runoob/g;
result=patt.test(str);
document.write("<br>返回值: " +  result);
</script>
    
</body>
</html>

定义和用法

test() 方法用于检测一个字符串是否匹配某个模式.

如果字符串中有匹配的值返回 true ,否则返回 false。

语法

RegExpObject.test(string)
原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/8397341.html