js正則表達式test()和exec()用法實例
更新時間:2015年01月22日 10:41:25 投稿:shichen2014
這篇文章主要介紹了js正則表達式test()和exec()用法,實例分析了test()函數和exec()函數在進行正則匹配時的使用技巧,需要的朋友可以參考下
本文實例講述了js正則表達式test()和exec()用法。分享給大家供大家參考。具體如下:
復制代碼 代碼如下:
<html>
<head>
<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>
</head>
<body>
<textarea rows="5" cols="30" name="content"></textarea><br />
<button onclick="t1();">正則測試(test函數)</button><br />
<button onclick="t2();">正則測試(exec函數)</button>
</body>
</html>
希望本文所述對大家的正則表達式學習有所幫助。
相關文章
JavaScript正則方法replace實現搜索關鍵字高亮顯示
這里介紹的是JavaScript的正則表達式的replace方法 ,和實現搜索關鍵字高亮的功能.先介紹一下正則表達式的replace方法,具體內容詳情大家參考下本文2017-09-09

