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
2 Answers
Reset to default 4Ok, 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
版权声明:本文标题:regex - Javascript Regular Expression Replace %2F with %21 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744664782a2618461.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论