简体   繁体   English

在JavaScript中从字符串的开头到结尾删除HTML内容组

[英]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. 我非常RegExRegEx解决方案,因此不需要浏览器附加技巧。
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.

相关问题 无法在 JavaScript 数组中提取开始和结束 HTML 标签组 - Not able to extract groups of start and end HTML tags in JavaScript array JavaScript 正则表达式 - 从开头和结尾删除空格 - JavaScript Regex - Remove Whitespace from Start and End 从 javascript 字符串中删除特定 HTML 标签及其内容 - Remove specific HTML tag with its content from javascript string 从字符串的开头和结尾删除换行符 - Remove line breaks from start and end of string HTML和Javascript:从字符串中删除包含动态内容的完整标签 - HTML and Javascript: Remove a complete tag with dynamic content inside from string 从字符串的开头和结尾删除多个逗号 - Remove multiple commas from start and end of string 如何从 javascript 中的字符串中删除包含内容的 HTML 标签(不是特定标签) - How to remove HTML tag (not a specific tag ) with content from a string in javascript 使用javascript从开始标记到结束标记中删除选定的html元素,并带有值 - Remove selected html element from start tag to end tag with values using javascript 如何从javascript中的字符串末尾删除9个字符 - How to remove 9 characters from the end of the string in javascript Javascript:如何从字符串末尾删除标点符号 - Javascript: how to remove punctuation from the end of a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM