Javascript Lookbehind Failed Regular Expressions

I hope this will have a fairly quick and easy answer. I use regular-expressions.info to help me get the correct regular expression to convert the URL-encoded ISO-8859-1 character ("% A3") to the URL-encoded UTF-8 character ("% C2% A3") .

In other words, I just want to exchange% A3 for% C2% A3 when% A3 no longer has the% C2 prefix.

So, I would think the following would work:

Regular Expression: (?!(\%C2))\%A3 Replace With: %C2%A3 

But this is not so, and I cannot understand why!

I assume that my syntax is just a bit wrong, but I can't figure it out! Any ideas?

FYI - I know that the following will work (and used this as a workaround in the meantime), but really want to understand why the first does not work.

 Regular Expression: ([^\%C2])\%A3 Replace With: $1%C2%A3 

TIA!

+1
source share
4 answers

Why not just replace ((%C2)?%A3) with %C2%A3 , making the prefix an optional part of the match? This means that you β€œreplace” the text with itself, even when it is already right, but I do not anticipate performance problems.

+4
source

Unfortunately, the syntax (?!) Is negative. As far as I know, JavaScript does not support negative lookbehind.

What would you do, one way or another, go ahead with the replacement and end the% C2% C2% A3 lines, but they can be easily converted in the second pass to the desired% C2% A3.

+4
source

You can replace

 (^.?.?|(?!%C2)...)%A3 

from

 $1%C2%A3 
+3
source

I suggest you use the Javascript String.replace functional form (see "Specifying a Function as a Parameter"). This allows you to put arbitrary logic, including state, if necessary, into a regular expression matching session. For your case, I would use a simpler regular expression that matches a superset of what you want, and then in the function call you can check if it matches your exact criteria, and if it doesn't, just return the matched string as it is.

The only problem with this approach is that if you have a match for potential matches, you have the option to skip the second match, as there is no way to return a value to tell the replace () method that this is not really the case.

+1
source

All Articles