Good day,
I would like to know if that’s possible using RegExp and if so, how.
Here is the problem:
In a string, I have multiple pattern that correspond to colors:
{r for dark red
{R for bright red
{G for bright green
{W for white
{* for black
... and so on...
Sometimes the data I’m parsing contains lot of useless color changes
Example:
{r{Gthis text is finally green
The {r is totally useless there. It wouldn’t if the string was {rSomething {Gthis text is finally green
I would like a way to match every {<color references> that is right before any other {<color references> and leaves the last one in place.
{r{Gthis text is green -> {Gthis text is green
That text is {R{rdark red -> That text is {rdark red
Is that an easy way to do that?
I am aware that this problem is really particular and will probably never help anyone else.
But help would be really appreciate.
Thank you!
PS: The codes is used in Javascript so if possible, I would like a solution working in Javascript.
Edit:
I must be able to do {Y{r{Gthis te{*xt {Y{yis green and get {Gthis te{*xt {yis green
Here’s a method that works. It works like this:
{followed by any other single character except a{. If there are only a specified set of color codes that you want to allow or there are more characters that are not allowable color codes, then those could also be added to the regex, but you didn’t specify that additional info.\{[^\{]. This is a{followed by any single character that is not a{. If you want it to only be alpha characters, it could be changed to this:\{[a-zA-Z].{{.This works for any character in the color code since you looked like you had a long list of valid single character codes. But, it protects against {{ which it will not treat as a color code.
or to allow just alpha characters to be a color code character, it would be this:
You can see a test fiddle here: http://jsfiddle.net/jfriend00/jNXUz/