Decoding URL parameters with JavaScript

后端 未结 5 706
离开以前
离开以前 2020-11-27 19:22

This should be a simple task, but I can\'t seem to find a solution.

I have a basic string that is being passed through as a query string parameter like this one:

5条回答
  •  猫巷女王i
    2020-11-27 20:04

    Yes it is true that decodeURIComponent function doesn't convert + to space. So you have to replace the + using replace function.

    Ideally the below solution works.

    var str_name = 'This+is+a+message+with+spaces';
    decodeURIComponent((str_name + '').replace(/\+/g, '%20'));
    

提交回复
热议问题