JavaScript test () method
Definition and Usage
test () method is used to detect whether a string matches a pattern.
If the string matching values return true, otherwise it returns false.
grammar
RegExpObject.test(string)
参数 | 描述 |
---|---|
string | 必需。要检测的字符串。 |
Browser Support
All major browsers support test () method
Examples
Examples
In the global search for the string "Hello" and "W3CSchool" string:
<script>
var str="Hello world!";
//look for "Hello"
var patt=/Hello/g;
var result=patt.test(str);
document.write("Returned value: " + result);
//look for "W3CSchool"
patt=/W3CSchool/g;
result=patt.test(str);
document.write("<br>Returned value: " + result);
</script>
var str="Hello world!";
//look for "Hello"
var patt=/Hello/g;
var result=patt.test(str);
document.write("Returned value: " + result);
//look for "W3CSchool"
patt=/W3CSchool/g;
result=patt.test(str);
document.write("<br>Returned value: " + result);
</script>
Examples of the above output:
Returned value: true
Returned value: false
Returned value: false
try it"
JavaScript RegExp Object