這篇文章主要介紹了js正則查找match()與替換replace()用法,實例分析了js中正則的查找match()與替換replace()的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了js中正則的查找match()與替換replace()的用法。分享給大家供大家參考。具體如下:
復制代碼代碼如下:
<html>
<head>
<script type="text/javascript">
//string.match(正則):正則查找字符串,返回符合正則的字符或字符串
function t1(){
var con = document.getElementsByName('content')[0].value;//需要查找的內容
var reg = //Bhi/B/g;//匹配中間有hi的單詞。g為模式增強符,表示全局匹配
alert(con.match(reg));
}
//string.replace(正則,用什么替換):返回被替換后的string
function t2(){
var con = document.getElementsByName('content')[0].value;//需要查找的內容
var reg = /<script.*<//script>/;//把javascript代碼替換為空
alert(con.replace(reg,''));
}
</script>
</head>
<body>
<textarea rows="5" cols="30" name="content"></textarea><br />
<button onclick="t1();">正則查找字符串match()</button><br />
<button onclick="t2();">正則查找字符串replace()</button><br />
</body>
</html>
希望本文所述對大家的正則表達式學習有所幫助。