[英]Remove HTML tags and its contents from a string - Javascript
Suppose I have the following string: const test = "This is outside the HTML tag. <title>How to remove an HTML element using JavaScript?</title>";
假设我有以下字符串:
const test = "This is outside the HTML tag. <title>How to remove an HTML element using JavaScript?</title>";
I'd like to remove the content within all HTML tags in that string.我想删除该字符串中所有 HTML 标签中的内容。 I have tried doing
test.replace(/(<([^>]+)>)/gi, '')
, but this only removes the HTML tags rather than all the content within it as well.我试过做
test.replace(/(<([^>]+)>)/gi, '')
,但这只会删除 HTML 标签,而不是其中的所有内容。 I would expect the outcome to only be 'This is outside the HTML tag.'.我希望结果只会是“这在 HTML 标签之外。”。
Is it possible to remove HTML tags and its contents within a string?是否可以删除字符串中的 HTML 标签及其内容?
You can replace everything between the two elements by putting a Wildcard character between two of your regex您可以通过在两个正则表达式之间放置一个通配符来替换两个元素之间的所有内容
const test = "This is outside the HTML tag. <title>How to remove an HTML element using JavaScript?</title>"; console.log(test.replace(/(<([^>]+)>).*(<([^>]+)>)/, ''))
Rather than trying to remove the HTML element via Regex, it's much more straightforward to create and populate a DOM Fragment using:与其尝试通过正则表达式删除 HTML 元素,不如使用以下方法创建和填充DOM 片段更直接:
let myDiv = document.createElement('div');
myDiv.innerHTML = test;
and then remove the <title>
element from that, using:然后从中删除
<title>
元素,使用:
myDivTitle = myDiv.querySelector('title');
myDiv.removeChild(myDivTitle);
Working Example (One Element):工作示例(一个元素):
const test = "This is outside the HTML tag. <title>How to remove an HTML element using JavaScript?</title>"; let myDiv = document.createElement('div'); myDiv.innerHTML = test; myDivTitle = myDiv.querySelector('title'); myDiv.removeChild(myDivTitle); const testAfter = myDiv.innerHTML; console.log(testAfter);
The above works for one element ( <title>
) but you stated:以上适用于一个元素(
<title>
)但你说:
I'd like to remove the content within all HTML tags in that string
我想删除该字符串中所有 HTML 标签中的内容
so let's try something more ambitious, using:所以让我们尝试一些更有野心的东西,使用:
myDiv.querySelectorAll('*')
Working Example (All Elements):工作示例(所有元素):
const test = "<title>How to remove an HTML element using JavaScript?</title> This is outside the HTML tag. <h1>Here we go...</h1> So is this. <p>This is going to save a lot of time trying to come up with regex patterns</p> This too."; let myDiv = document.createElement('div'); myDiv.innerHTML = test; myDivElements = myDiv.querySelectorAll('*'); for (myDivElement of myDivElements) { myDiv.removeChild(myDivElement); } const testAfter = myDiv.innerHTML; console.log(testAfter);
You should try like this:你应该这样尝试:
var html = "<p>Hello, <b>Frields</b>";
var div = document.createElement("div");
div.innerHTML = html;
alert(div.innerText); // Hello, Frields
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.