V
V S Rawat
I was trying to use back-to-back replace functions to convert a url:
str1 =
str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2
6","&");
It didn't replace all 4 types of strings.
Then, I googled and found this suggestion of some JavaScript Tutorials,
so I used replace with a regex with a global switch:
str1 =
str.replace(/%2F/g,"/").replace(/%3F/g,"?").replace(/%3D/g,"=").replace(
/%26/g,"&");
and it did replace all the occurances of all the strings.
OK. my problem is solved, but I am curious why should the first method
not work?
Thanks.
--
str1 =
str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2
6","&");
It didn't replace all 4 types of strings.
Then, I googled and found this suggestion of some JavaScript Tutorials,
so I used replace with a regex with a global switch:
str1 =
str.replace(/%2F/g,"/").replace(/%3F/g,"?").replace(/%3D/g,"=").replace(
/%26/g,"&");
and it did replace all the occurances of all the strings.
OK. my problem is solved, but I am curious why should the first method
not work?
Thanks.
--