I have the following string variable and I want to remove all a
tags with its content from the string.
我有以下字符串变量,我想从字符串中删除所有带有内容的标记。
var myString = "<table><tr><td>Some text ...<a href='#'>label...</a></td></tr></table>";
myString += "<table><tr><td>Some text ...<a href='#'>label...</a></td></tr></table>";
myString += "<table><tr><td>Some text ...<a href='#'>label...</a></td></tr></table>";
I have checked this Remove HTML content groups from start to end of string in JavaScript question's answers but it is for all tags.
我已经检查了这个在JavaScript问题的答案中从字符串的开头到结尾删除HTML内容组,但它适用于所有标签。
Thank you
3 个解决方案
#1
5
You should avoid parsing HTML using regex. Here is a way of removing all the <a>
tags using DOM
:
您应该避免使用正则表达式解析HTML。这是一种使用DOM删除所有标签的方法:
// your HTML text
var myString = '<table><tr><td>Some text ...<a href="#">label...</a></td></tr></table>';
myString += '<table><tr><td>Some text ...<a href="#">label...</a></td></tr></table>'
myString += '<table><tr><td>Some text ...<a href="#">label...</a></td></tr></table>'
// create a new dov container
var div = document.createElement('div');
// assing your HTML to div's innerHTML
div.innerHTML = myString;
// get all <a> elements from div
var elements = div.getElementsByTagName('a');
// remove all <a> elements
while (elements[0])
elements[0].parentNode.removeChild(elements[0])
// get div's innerHTML into a new variable
var repl = div.innerHTML;
// display it
console.log(repl)
/*
<table><tbody><tr><td>Some text ...</td></tr></tbody></table>
<table><tbody><tr><td>Some text ...</td></tr></tbody></table>
<table><tbody><tr><td>Some text ...</td></tr></tbody></table>
*/
#2
3
Here's the code. The regex /<a.*>.*?<\/a>/ig
fits well for your data.
这是代码。正则表达式/
var myString = "<table><tr><td>Some text ...<a href='#'>label...</a></td></tr></table";
myString += "<table><tr><td>Some text ...<a href='#'>label...</a></td></tr></table";
myString += "<table><tr><td>Some text ...<a href='#'>label...</a></td></tr></table";
console.log(myString);
var anchorTagsRemoved = myString.replace(/<a.*>.*?<\/a>/ig,'');
console.log(anchorTagsRemoved);
#3
0
var myString = "<table><tr><td>Some text ...<a href='#'>label...</a></td></tr></table>";
myString += "<table><tr><td>Some text ...<a href='#'>label...</a></td></tr></table>";
myString += "<table><tr><td>Some text ...<a href='#'>label...</a></td></tr></table>";
el = document.createElement('div');
el.innerHTML = myString;
var output = document.getElementById('output');
el.querySelectorAll('a').forEach(function(item, index){
item.parentNode.removeChild(item);
})
output.innerText = el.innerHTML;
<h3>Output:</h3>
<pre id='output'></pre>
#1
5
You should avoid parsing HTML using regex. Here is a way of removing all the <a>
tags using DOM
:
您应该避免使用正则表达式解析HTML。这是一种使用DOM删除所有标签的方法:
// your HTML text
var myString = '<table><tr><td>Some text ...<a href="#">label...</a></td></tr></table>';
myString += '<table><tr><td>Some text ...<a href="#">label...</a></td></tr></table>'
myString += '<table><tr><td>Some text ...<a href="#">label...</a></td></tr></table>'
// create a new dov container
var div = document.createElement('div');
// assing your HTML to div's innerHTML
div.innerHTML = myString;
// get all <a> elements from div
var elements = div.getElementsByTagName('a');
// remove all <a> elements
while (elements[0])
elements[0].parentNode.removeChild(elements[0])
// get div's innerHTML into a new variable
var repl = div.innerHTML;
// display it
console.log(repl)
/*
<table><tbody><tr><td>Some text ...</td></tr></tbody></table>
<table><tbody><tr><td>Some text ...</td></tr></tbody></table>
<table><tbody><tr><td>Some text ...</td></tr></tbody></table>
*/
#2
3
Here's the code. The regex /<a.*>.*?<\/a>/ig
fits well for your data.
这是代码。正则表达式/
var myString = "<table><tr><td>Some text ...<a href='#'>label...</a></td></tr></table";
myString += "<table><tr><td>Some text ...<a href='#'>label...</a></td></tr></table";
myString += "<table><tr><td>Some text ...<a href='#'>label...</a></td></tr></table";
console.log(myString);
var anchorTagsRemoved = myString.replace(/<a.*>.*?<\/a>/ig,'');
console.log(anchorTagsRemoved);
#3
0
var myString = "<table><tr><td>Some text ...<a href='#'>label...</a></td></tr></table>";
myString += "<table><tr><td>Some text ...<a href='#'>label...</a></td></tr></table>";
myString += "<table><tr><td>Some text ...<a href='#'>label...</a></td></tr></table>";
el = document.createElement('div');
el.innerHTML = myString;
var output = document.getElementById('output');
el.querySelectorAll('a').forEach(function(item, index){
item.parentNode.removeChild(item);
})
output.innerText = el.innerHTML;
<h3>Output:</h3>
<pre id='output'></pre>