admin管理员组

文章数量:1391925

I need a javascript/regex to replace %2F with %21

This is so that I can pass forward slashes through a GET parameter after applying encodeURIComponent() to a URL.

When it reaches the server side I'll convert back from ! to /

This isn't an ideal solution, but nothing else seems to work with my rewrite rules.

I need a javascript/regex to replace %2F with %21

This is so that I can pass forward slashes through a GET parameter after applying encodeURIComponent() to a URL.

When it reaches the server side I'll convert back from ! to /

This isn't an ideal solution, but nothing else seems to work with my rewrite rules.

Share Improve this question asked Dec 7, 2015 at 19:19 Amy NevilleAmy Neville 10.6k13 gold badges61 silver badges98 bronze badges 9
  • If you wanted, it might be simpler just to loop through the string and replace them yourself. Regexes are generally used for more plicated string matching than this. – Nate Young Commented Dec 7, 2015 at 19:24
  • would it be more efficient to loop do you think? – Amy Neville Commented Dec 7, 2015 at 19:25
  • Why loop? Use replace. I don't see any issue with using a regex here, though. – Dave Newton Commented Dec 7, 2015 at 19:27
  • 1 @AmyNeville You can use a regex with replace and use the global flag. There's no reason to loop. – Dave Newton Commented Dec 7, 2015 at 19:32
  • 1 I updated it to use regex to do a global replace. Please don't use a loop! – spozun Commented Dec 7, 2015 at 19:33
 |  Show 4 more ments

2 Answers 2

Reset to default 4

Ok, I have solved this problem and it took a great deal of research as I'm not as talented as some people on here. I thought I'd share the solution anyway.

Essentially the server will prematurely decode %2F as soon as it is used, so you end up with a path that is totally wrong.

What you have to do is replace %2F with %252F at the client side.

x = x.replace(/%2F/gi, "%252F");

This is the double encoded form of %2F.

So when it reaches the server it prematurely gets decoded to %2F instead of a forward slash.

You're wele.

Did you try String.replace?

x = x.replace(/%2F/gi, "%21");

本文标签: regexJavascript Regular Expression Replace 2F with 21Stack Overflow