如何用JavaScript将%20替换为+?

时间:2018-11-05 15:34:14

标签: javascript string

我有一个问题。 在此代码中,当用户用麦克风讲话时,我使用+ tag设置搜索词。

var searching = function(tag) {
            var string = "Procurando "+tag;
            var str = string.split("");
            var el = document.getElementById('liya');
            (function animate() {
                str.length > 0 ? el.innerHTML += str.shift() : clearTimeout(running); 
                var running = setTimeout(animate, 30);
            })();

            setTimeout(function () {
                $(location).attr('href', 'https://yazui.co/liya/search.php?q='+tag)
            }, 3000);            
        };

没关系!但是结果是,当有很多单词时url显示为%20

search.php?q=words%20words%20words

如何在此代码中将%20更改为+?

我需要该网址返回

search.php?q=words+words+words

1 个答案:

答案 0 :(得分:0)

您可以这样做。

var searching = function(tag) {
            var string = "Procurando "+tag;
            var str = string.split("");
            var el = document.getElementById('liya');
            (function animate() {
                str.length > 0 ? el.innerHTML += str.shift() : clearTimeout(running); 
                var running = setTimeout(animate, 30);
            })();

            setTimeout(function () {
                $(location).attr('href', 'https://yazui.co/liya/search.php?q='+tag.replace(/%20/g, "+");)
            }, 3000);            
        };