最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

urlencode - How to decode parameter with space in URL using javascript? - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a ment  | 

3 Answers 3

Reset to default 10
function 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," ");  
发布评论

评论列表(0)

  1. 暂无评论