我正在使用Algolia场所搜索,我想将其从表单输入值传递到URL。
替换“%20”的最佳方法是:
mywebsite.com/location_name=Fort%20Lauderdale,%20Florida,%20United%20States%20of%20America
和“,”与“-”还是完全删除?
mywebsite.com/?location_name=Fort%20Lauderdale,%20Florida,%20United%20States%20of%20America
我想要这样:
mywebsite.com/?location_name=Fort-Lauderdale-Florida-United-States-of-America
或者也许只是:
mywebsite.com/?location_name=Fort-Lauderdale
答案 0 :(得分:0)
将String.prototype.replace
与简单的正则表达式一起使用,如下所示:
const str = "mywebsite.com/?location_name=Fort%20Lauderdale,%20Florida,%20United%20States%20of%20America";
const modified = str.replace(/\,~|\%20/g, "-");
console.log(modified);
上面的正则表达式与逗号或URL编码的空格匹配。
答案 1 :(得分:0)
在php
$link = "http://mywebsite.com/?location_name=Fort%20Lauderdale,%20Florida,%20United%20States%20of%20America";
echo str_replace(array(',', '%20'), array('','-'), $link);