JS regular expressions test() and exec() usage examples

Time:2020-5-15

The examples of this article describe the usage of JS regular expressions test() and exec(). Share with you for your reference. The details are as follows:

Copy codeThe code is as follows:
<html>
<head>
<script type=”text/javascript”>
//Regular. Test (content), return true or false
function t1(){
Var con = document.getelementsbyname (‘content ‘) [0]. Value; / / content to find
Var reg = / hi /; / / content to match
 alert(reg.test(con));
}
//Regular. Exec (content), return matching content
function t2(){
Var con = document.getelementsbyname (‘content ‘) [0]. Value; / / content to find
Var reg = / \ BHI \ W + /; / / content to match
 alert(reg.exec(con));
}
</script>
</head>
<body>
<textarea rows=”5″ cols=”30″ name=”content”></textarea><br />
< button onclick = “t1();” > regular test (test function) < / button > < br / >
< button onclick = “t2();” > regular test (exec function) < / button >
</body>
</html>

I hope that this article will be helpful for your regular expression learning.