In an application I'm writing, at some point, I want to be able to parse a tag-like string with RegExp so that it is modified, such as:
"{b}This is BOLD{/b}".replace(/\{b\}(.*?)\{\/b\}/gi, "00b3f[ $1 00b3d]");
// Returns "00b3f[ This is BOLD 00b3d]"
I am able to do this easily, but it gets complicated when a more complex string is passed to the function, for example:
"{red} This is RED {red} This also should be red {/red} and this {/red}"
.replace(/\{red\}(.*?)\{\/red\}/gi, "00b4f[ $1 00b4d]");
// Returns:
// "00b4f[ This is RED {red} This also should be red 00b4d] and this {/red}"
// Where the output should be:
// "00b4f[ This is RED 00b4f[ This also should be red 00b4d] and this 00b4d]"
I would like to solve this problem with a simple RegExp but I can't find a way to do it! I think I could do this with a while-loop but it would get too messy. Any suggestions?