欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript實現(xiàn)正則去除a標簽并保留內(nèi)容的方法【測試可用】

 更新時間:2018年07月18日 11:44:17   作者:@vimac  
這篇文章主要介紹了JavaScript實現(xiàn)正則去除a標簽并保留內(nèi)容的方法,結(jié)合實例形式詳細分析了javascript針對a標簽及span標簽的正則匹配相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了JavaScript實現(xiàn)正則去除a標簽并保留內(nèi)容的方法。分享給大家供大家參考,具體如下:

一、問題:

有如下HTML代碼,要求用正則去除a標簽,只留下內(nèi)容 //www.dbjr.com.cn

復(fù)制代碼 代碼如下:
<a href="http://www.dbjr.com.cn/" style="box-sizing: border-box; color: rgb(51, 51, 51); text-decoration: none; transition: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1); -webkit-transition: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1); max-width: 100%; transparent;"><span data-wiz-span="data-wiz-span" style="box-sizing: border-box; max-width: 100%; font-size: 14pt;">//www.dbjr.com.cn</span></a>

二、解決方法:

這里使用可刪除a標簽與span標簽的正則語句,如下:

(<\/?a.*?>)|(<\/?span.*?>)

具體js正則語句:

str.replace(/(<\/?a.*?>)|(<\/?span.*?>)/g, '');

完整測試代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js正則刪除a標簽并保留內(nèi)容</title>
</head>
<body>
<a href="http://www.dbjr.com.cn/" style="box-sizing: border-box; color: rgb(51, 51, 51); text-decoration: none; transition: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1); -webkit-transition: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1); max-width: 100%; transparent;"><span data-wiz-span="data-wiz-span" style="box-sizing: border-box; max-width: 100%; font-size: 14pt;">//www.dbjr.com.cn</span></a>
<script>
var str=document.getElementsByTagName('a')[0].outerHTML;
console.log("正則刪除之前:"+str);
str=str.replace(/(<\/?a.*?>)|(<\/?span.*?>)/g, '');
console.log("正則刪除之后:"+str);
</script>
</body>
</html>

使用在線HTML/CSS/JavaScript代碼運行工具http://tools.jb51.net/code/HtmlJsRun,測試結(jié)果如下:

PS:這里再為大家提供2款非常方便的正則表達式工具供大家參考使用:

JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript

正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript正則表達式技巧大全》、《JavaScript替換操作技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript中json操作技巧總結(jié)》、《JavaScript錯誤與調(diào)試技巧總結(jié)》及《JavaScript數(shù)學(xué)運算用法總結(jié)

希望本文所述對大家JavaScript程序設(shè)計有所幫助。

相關(guān)文章

最新評論