[英]Remove HTML content groups from start to end of string in JavaScript
I need a regex that could remove the full tag from start to end. 我需要一个可以从头到尾删除完整标签的正则表达式。
For eg.: 例如:
For the given string: 对于给定的字符串:
var str = "Hello <script> console.log('script tag') </script> World";
I need an output: 我需要一个输出:
"Hello World" // with full script tag including inner content removed
I am very specific for only the RegEx
solution, so don't need browser append tricks. 我非常
RegEx
于RegEx
解决方案,因此不需要浏览器附加技巧。
Kindly notify if this is not possible at all. 如果根本不可能,请通知。
I tried this and its variations: 我试过这个及其变化:
inputString.replace( /<\/?[^>]+(>|$)/g, "" );
But this is not achieving what I want and is removing only the tag elements, leaving the inner content. 但这并没有实现我想要的,只删除标签元素,留下内在的内容。 What
RegEx
groups should I create in the expression? 我应该在表达式中创建哪些
RegEx
组?
I do not need to address stuff like type="text/javascript"
, as they are already filtered before I receive the string. 我不需要解决类似
type="text/javascript"
,因为它们在我收到字符串之前已经过滤了。 No jQuery plz. 没有jQuery plz。 (I need to store the RegEx as a property to my filter object).
(我需要将RegEx存储为我的过滤器对象的属性)。
Help appreciated. 帮助赞赏。
This is pure regex solution: 这是纯正的正则表达式解决方案:
var str = "Hello <script> console.log('script tag') </script> World";
var repl = str.replace(/<([^.]+)>.*?<\/\1>/ig, '');
//=> "Hello World"
with an assumption that there is no <
OR >
between opening and closing tags. 假设开始和结束标签之间没有
<
OR >
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.