如何去掉 文章段落中的<a></a>
有一个段落
你好, <a href= "http://www.xxx.com "> 链接 </a> ,测试
现在想把这个字符串变为 你好,链接,测试
也就是去掉全文中所有带链接的部分应该如何写正则表达式呢
------解决方案-------------------- <html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> new page </title>
<script>
function a(){
var aObj=document.getElementsByTagName( "a ");
for(var i=0;i <aObj.length;i++){
aObj[i].parentNode.removeChild(aObj[i]);
}
}
</script>
</head>
<body onload= "a() ">
你好, <a href= "http://www.xxx.com "> 链接 </a> ,测试
</body>
</html>
------解决方案-------------------- <script language=javascript>
var str= '你好, <a href= "http://www.xxx.com "> 链接 </a> ,测试 '
re=/ <\/?a[^> ]*> /g
alert(str.replace(re, " "))
</script>
------解决方案--------------------一楼滴解法有漏洞,应该倒序循环!
斑竹滴解法可行,但是无法还原链接!
俺给个循环解法,可自由停用也可还原链接!
只为好玩,仅供参考!
<!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>
<title> remove and resume attribute </title>
<meta name= "generator " content= "editplus " />
<meta name= "author " content= "yixianggao@126.com " />
<meta name= "keywords " content= "javascript " />
<meta name= "description " content= "for javascript region of csdn " />
</head>
<body>
你好, <a href= "http://www.no1.com "> 链接1 </a> ,测试 <br />
你好, <a href= "http://www.no2.com "> 链接2 </a> ,测试 <br />
你好, <a href= "http://www.no3.com "> 链接3 </a> ,测试 <br />
<input type= "button " id= "btnFreeze " value= "Freeze Href " onclick= "freezeHref() " />
<input type= "button " id= "btnResume " value= "Resume Href " onclick= "resumeHref() " />
</body>
<script type= "text/javascript ">
<!--
// 冻结 href 属性。
function freezeHref()
{
var colA=document.getElementsByTagName( "a ");
for(var i=0; i <colA.length; i++)
{
if (colA[i].href)
{
colA[i].originalHref = colA[i].href;
colA[i].removeAttribute( "href ");
}
}
}
// 还原 href 属性。
function resumeHref()
{
var colA=document.getElementsByTagName( "a ");
for(var i=0; i <colA.length; i++)
{
if (colA[i].originalHref)
{
colA[i].href = colA[i].originalHref;
}
}
}
//-->
</script>
</html>
------解决方案--------------------是符可以用正则表达式试试:
var str= '你好, <a href= "http://www.xxx.com "> 链接 </a> ,测试 ';
var reg = /([^ <]*) <[^> ]*> ([^ <]*) <[^ <]*> ([\s\S]*