-2

I have a string such as

var string = 'blabla <a href="http://www.url.com" <custom-tag>attribute-x="blabla"</custom-tag>>an url</a> blabla <custom-tag>blabla</custom-tag> blablabla between tags <custom-tag><a href="http://www.anotherurl.com">blabla</a></custom-tag>';

where I want to strip all the custom-tag tags. The problem is that the deletion needs to happen sequential (I think) as multiple instances occur and the tag can contain other tags or be included in other tags itself.

At the moment, the best solution I have is

var deleteTag = '<custom-tag>.*<\/custom-tag>';
string= string.replace(new RegExp(deleteTag , 'g'), '');

which leaves me with

blabla <a href="http://www.url.com" 

instead of

blabla <a href="http://www.url.com">an url</a> blabla blablabla between tags

Should I implement a loop or is there a way to do this with RegExp?

Thanks!

PS: It is not possible for me to parse my string as HTML as it contains tags within tags and would thus render false HTML (it is part of a templating module in our software so the string goes through some iterations after which it eventually does end up as HTML). So it is not a duplicate of questions such as Remove specific HTML tag with its content from javascript string

Mats Raemen
  • 1,679
  • 1
  • 27
  • 39
  • 1
    either regex or if it gets complicated you could chuck the string in an actual dom parser. Something like https://github.com/jsdom/jsdom – The Fool Sep 23 '20 at 09:46
  • _"it is part of a templating module in our software"_ - You might want to fix the module instead so it doesn't output invalid html/xml (`attribute-x="blabla">an url`). – Andreas Sep 23 '20 at 09:51
  • Hi @Andreas, the eventual result is perfectly valid HTML However, as it is a complex module, not each iteration produces valid HTML (tags within a tag as you can see) so parsing the raw template into HTML is impossible – Mats Raemen Sep 23 '20 at 10:01

1 Answers1

-1

You can avoid this particular issue by using a lazy match in your regex instead of a greedy one. Try this:

var deleteTag = '<custom-tag>.*?<\/custom-tag>';
string= string.replace(new RegExp(deleteTag , 'g'), '');

If you have nested <custom-tag>, though, regex is probably not the tool for the job.

daniloxxv
  • 702
  • 4
  • 10