javascript encodeURIComponent and converting spaces to + symbols

前端 未结 2 1725
栀梦
栀梦 2020-12-06 04:47

I would like to encode my URL, but I want to convert spaces to plus symbols.

This is what I attempted to do...

var search = \"Testing this here &         


        
2条回答
  •  春和景丽
    2020-12-06 05:36

    encodeURIComponent(search).replace(/%20/g, "+");
    

    What you're doing wrong here is that first you convert spaces to pluses, but then encodeURIComponent converts pluses to "%2B".

提交回复
热议问题