[英]HTML and Javascript: Remove a complete tag with dynamic content inside from string
I have a string that conains HTML. 我有一个包含HTML的字符串。 In this HTML I have a textbox with text inside:
在此HTML中,我有一个文本框,其中包含文本:
<div class="aLotOfHTMLStuff"></div>
<textbox>This textbox must be terminated! Forever!</textbox>
<div class="andEvenMoreHTMLStuff"></div>
Now I want to remove the textbox from that string, including the text inside. 现在,我想从该字符串中删除文本框,包括其中的文本。 The desired result:
预期结果:
<div class="aLotOfHTMLStuff"></div>
<div class="andEvenMoreHTMLStuff"></div>
How can I achieve it? 我该如何实现? The two main problems: It is a string and not part of the DOM and the content inside the textbox is dynamic.
两个主要问题:它是一个字符串,不是DOM的一部分,并且文本框中的内容是动态的。
Here is an example that will look for the opening and closing tags in the string and replace anything in between. 这是一个示例,该示例将查找字符串中的开始和结束标签,并替换两者之间的任何内容。
const template = document.querySelector('#html') const str = template.innerHTML function removeTagFromString(name, str) { const reg = new RegExp('<' + name + '.*>.*<\\/'+ name +'.*>\\\\n*', 'gm') return str.replace(reg, '') } console.log('before', str) console.log('after', removeTagFromString('textbox', str))
<template id="html"> <div class="aLotOfHTMLStuff"></div> <textbox>This textbox must be terminated! Forever!</textbox> <div class="andEvenMoreHTMLStuff"></div> </template>
If it is text string not HTML, you can convert it to DOM: 如果它是文本字符串而不是HTML,则可以将其转换为DOM:
var str = '<div class="aLotOfHTMLStuff"></div><textbox>This textbox must be terminated! Forever!</textbox><div class="andEvenMoreHTMLStuff"></div>';
var $dom = $('<div>' + str + '</div>');
Then remove element from DOM: 然后从DOM中删除元素:
$dom.find('textbox').remove();
If you need, can get string back: 如果需要,可以返回字符串:
console.log($dom.html());
Try this: 尝试这个:
var string = '<div class="aLotOfHTMLStuff"></div><textbox>This textbox must be terminated! Forever!</textbox><div class="andEvenMoreHTMLStuff"></div>'; if ( string.includes("<textbox>") ) { var start = string.indexOf("<textbox>"); var stop = string.indexOf("</textbox>"); string = string.replace(string.slice(start, stop+10), ""); } console.log(string);
You can use parseHTML to convert string to html, like.. 您可以使用parseHTML将字符串转换为html,例如。
var str = '<div class="aLotOfHTMLStuff"></div><textbox>This textbox must be terminated! Forever!</textbox><div class="andEvenMoreHTMLStuff"></div>';
var a = $.parseHTML(str);
var newstr = "";
a.forEach(function(obj) {
if ($(obj).prop('tagName').toLowerCase() != "textbox") {
newstr += $(obj).prop("outerHTML")
}
});
It's very simple and you can remove any tag in future by just replacing "textbox"
这非常简单,以后您只需替换
"textbox"
就可以删除任何标签
It is a string and not part of the DOM and the content inside the textbox is dynamic.
它是一个字符串,不是DOM的一部分,并且文本框中的内容是动态的。
So that html is in a js variable of type string? 这样的HTML是在字符串类型的js变量? like this?:
像这样?:
var string = '<div class="aLotOfHTMLStuff"></div><textbox>This textbox must be terminated! Forever!</textbox><div class="andEvenMoreHTMLStuff"></div>';
So in that case you could use .replace();
因此,在这种情况下,您可以使用
.replace();
like this: 像这样:
string = string.replace('<textbox>This textbox must be terminated! Forever!</textbox>', '');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.