[英]How do I strip <link> tag from javascript String object using Regex or some better alternative?
Here is code: 这是代码:
<div class="rightColoumn" id="divreports">
<link href="/favicon.ico" rel="shortcut icon" type="image/x-icon">
<link href="../ClientScript/General.css" rel="stylesheet" type="text/css">
</div>
I wish I could remove lines completely. 我希望我可以完全删除行。
javascript String object strObject contains this html. javascript字符串对象strObject包含此html。
How about just parsing it as what it is, HTML 仅将其解析为HTML怎么样
var strObject = '<div class="rightColoumn" id="divreports"><link href="/favicon.ico" rel="shortcut icon" type="image/x-icon"><link href="../ClientScript/General.css" rel="stylesheet" type="text/css"></div>';
var parser = new DOMParser();
var doc = parser.parseFromString(strObject, "text/html");
var parent = doc.getElementById('divreports');
var links = parent.getElementsByTagName('link');
for (var i=links.length; i--;) parent.removeChild(links[i]);
here's another way to remove < link tag lines: 这是删除<链接标记行的另一种方法:
strObject.replace(/^.*<link.*$/mg, "");
It will work if your string has line break '\\n' at the end of each line. 如果您的字符串在每行末尾都有换行符'\\ n',则它将起作用。
Here try this way to remove html tag 在这里尝试这种方式删除html标签
//link
var reg= /<link\b[^>]*?>/gi
return this
.replace(reg, '$1<link href="http://$2">$2>')
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.