I try to extract parameter from URL, but one parameter has space which is replaced with "+", so the parameter I extract is "iphone+4", but actually it is "iphone 4", how can I convert to the second form, decodeURIComponent does not work here.
I try to extract parameter from URL, but one parameter has space which is replaced with "+", so the parameter I extract is "iphone+4", but actually it is "iphone 4", how can I convert to the second form, decodeURIComponent does not work here.
Share Improve this question edited Jun 9, 2011 at 15:08 kapa 78.7k21 gold badges165 silver badges178 bronze badges asked Apr 15, 2011 at 7:07 zjffduzjffdu 28.8k50 gold badges119 silver badges170 bronze badges3 Answers
Reset to default 10function decodeParameter(param) {
return decodeURIComponent(param.replace(/\+/g, ' '));
}
"iPhone+4".replace("+"," ");
That should do it?
It is an ambiguous thing, because you don't really know whether the +
means a space or an actual plus sign. If you are also responsible for creating the URLs, you can solve this by using an appropriate URL encoding function which will use %20 to encode spaces. If you are just collecting them from somewhere else, well, you are left with the option of assuming that every +
means a space :).
You can replace all +
s using this code:
your_text.replace(/\+/g," ");