4

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.

Jongware
  • 22,200
  • 8
  • 54
  • 100
Om Shankar
  • 7,989
  • 4
  • 34
  • 54
  • 1
    Required read for everyone who asks about parsing HTML with regular expressions: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Philipp Oct 28 '13 at 10:23
  • 3
    Have bookmarked that answer long back in my Stack folder, don't want to simply increase its view. I don't need a Chuck Norris RegEx for matching tags. My conditions are limited. Given a limited set of conditions, it can be done! – Om Shankar Oct 28 '13 at 10:28

1 Answers1

4

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    I didn't know that we can use `\1` in that very expression, Have always used in the expression after the comma. Worked. Thanks! – Om Shankar Oct 28 '13 at 10:47
  • Yes `\1` is back-reference that can be used in the regex itself. – anubhava Oct 28 '13 at 10:48
  • If I want to remove the specific tag with its content, could you please help me? – Gulmuhammad Akbari Jul 23 '17 at 06:01
  • @anubhava it doesn't work `str.replace(/<(a)\s*[^>]*>.*?<\/\1>/ig, '')` and I want to remove all `a` tags with its content please help me to solve this. – Gulmuhammad Akbari Jul 23 '17 at 06:13
  • @GulmuhammadAkbari: Comments is not the right place to ask new questions. Please post a new question and you will get answers. – anubhava Jul 23 '17 at 06:36
  • @anubhava I asked the question could you please answer it? appreciate the https://stackoverflow.com/questions/45262311/remove-specific-html-tag-with-its-content-from-javascript-string – Gulmuhammad Akbari Jul 23 '17 at 06:49