Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-06-01 09:54:15
Original
1121 people have browsed it

本文实例讲述了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>
Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!