我想对我的 URL 进行编码,但我想将空格转换为加号。
这就是我试图做的......
var search = "Testing this here &";
encodeURIComponent(search.replace(/ /gi,"+"));
输出是 Testing%2Bthis%2Bhere%2B%26
但我希望它是 Testing+this+here+%26
我尝试替换%20
的空格将其转换为加号,但这似乎不起作用。谁能告诉我我在这里做错了什么?
最佳答案
encodeURIComponent(search).replace(/%20/g, "+");
您在这里做错的是首先您将空格转换为加号,然后 encodeURIComponent
将加号转换为 "%2B"
.
关于javascript encodeURIComponent 并将空格转换为 + 符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10857590/