Home  >  Article  >  Web Front-end  >  javascript 正则表达式test()和exec()用法

javascript 正则表达式test()和exec()用法

WBOY
WBOYOriginal
2016-06-01 09:54:151130browse

本文实例讲述了js正则表达式test()和exec()用法。分享给大家供大家参考。具体如下:

<code class="language-html">

<script type="text/javascript">
//正则.test(内容),返回true或false
function t1(){
 var con = document.getElementsByName('content')[0].value;//需要查找的内容
 var reg = /hi/;//需要匹配的内容
 alert(reg.test(con));
}
//正则.exec(内容),返回匹配的内容
function t2(){
 var con = document.getElementsByName('content')[0].value;//需要查找的内容
 var reg = /\bhi\w+/;//需要匹配的内容
 alert(reg.exec(con));
}
</script>


<textarea rows="5" cols="30" name="content"></textarea><br>
<button onclick="t1();">正则测试(test函数)</button><br>
<button onclick="t2();">正则测试(exec函数)</button>

</code>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn