在JavaScript中从字符串的开头到结尾删除HTML内容组 [英] Remove HTML content groups from start to end of string in JavaScript
问题描述
我需要一个可以从开始到结束删除完整标记的正则表达式。
例如:
对于给定的字符串:
var str =Hello< script> console.log('script tag')< / script> World;
我需要输出:
Hello World//使用包含内容的完整脚本标记
我只针对 RegEx
解决方案,因此不需要浏览器追加技巧。
请通知if这是不可能的。
我试过这个和它的变化:
inputString.replace(/< \ /?[^>] +(> | $)/ g,);
但是这并没有达到我想要的效果,只移除了标签元素,留下了内容。我应该在表达式中创建 RegEx
组吗?
我不需要像 type =text / javascript
,因为它们在我收到字符串之前已经过滤。
没有jQuery plz。 (我需要将RegEx作为属性存储到我的过滤器对象中)。
帮助表示赞赏。
这是纯正则表达式解决方案:
var str =Hello< script> console。 log('script tag')< / script> World';
var repl = str.replace(/<([^。+)>。*?< \ / \ 1> / ig,'');
// =>假设没有<
,Hello World
code> OR >
开始和结束标记之间。
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.
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?
I do not need to address stuff like type="text/javascript"
, as they are already filtered before I receive the string.
No jQuery plz. (I need to store the RegEx as a property to my filter object).
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.
这篇关于在JavaScript中从字符串的开头到结尾删除HTML内容组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!