-1

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

3
  • 1
    either regex or if it gets complicated you could chuck the string in an actual dom parser. Something like github.com/jsdom/jsdom Commented Sep 23, 2020 at 9: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 (<a href="http://www.url.com" <custom-tag>attribute-x="blabla"</custom-tag>>an url</a>). Commented Sep 23, 2020 at 9: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 Commented Sep 23, 2020 at 10:01

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

3 Comments

this solves the problem indeed, thanks! I will have a look into whether there are better routes to take to achieve this
@MatsRaemen Yes. Fix the template module.
This helped with my problem. Thanks. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.