I have a string with tags.
"<blue>Hello, <red>world</red>!</blue>"
. And I have to get this string without tags using RegExp.
I wrote this code, but I think that it can be easier.
str.split('<red>').join('').split('</red>').join('').split('</blue>').join('').split('<blue>').join('');
Please, help me!
Use .replace(/<\/?(red|blue)>/g, "")
:
console.log( "<blue>Hello, <red>world</red>!</blue>".replace(/<\/?(red|blue)>/g, "") )
If you want to match also case insensitive versions of the tags, you just need to insert i
after /g
. This will not work with attributes or spaces before or after the tag name. If you need it, comment on this answer and I will edit it.