JavaScript test() 方法

weixin_33763244發表於2018-02-01

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)

 

相關文章