Javascript 用“+”替换字符串中的所有空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3794919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:11:48  来源:igfitidea点击:

Replace all spaces in a string with '+'

javascriptstring

提问by DaveDev

I have a string that contains multiple spaces. I want to replace these with a plus symbol. I thought I could use

我有一个包含多个空格的字符串。我想用加号替换这些。我以为我可以用

var str = 'a b c';
var replaced = str.replace(' ', '+');

but it only replaces the first occurrence. How can I get it replace all occurrences?

但它只替换第一次出现。我怎样才能让它取代所有的出现?

回答by Dagg Nabbit

Here's an alternative that doesn't require regex:

这是一个不需要正则表达式的替代方案:

var str = 'a b c';
var replaced = str.split(' ').join('+');

回答by Nick Craver

You need the /g(global) option, like this:

您需要/g(全局)选项,如下所示:

var replaced = str.replace(/ /g, '+');

You can give it a try here. Unlike most other languages, JavaScript, by default, only replaces the first occurrence.

你可以在这里试一试。与大多数其他语言不同,JavaScript 在默认情况下仅替换第一次出现。

回答by epascarello

var str = 'a b c';
var replaced = str.replace(/\s/g, '+');

回答by Jitendra Pancholi

You can also do it like

你也可以这样做

str = str.replace(/\s/g, "+");

Have a look to the fiddle

看看小提琴

回答by Andrew

Use global search in the string. g flag

在字符串中使用全局搜索。g旗

str.replace(/\s+/g, '+');

source: replaceAll function

来源:replaceAll 函数

回答by Mark Byers

Use a regular expression with the gmodifier:

使用带有g修饰符的正则表达式:

var replaced = str.replace(/ /g, '+');

From Using Regular Expressions with JavaScript and ActionScript:

在 JavaScript 和 ActionScript 中使用正则表达式

/g enables "global" matching. When using the replace() method, specify this modifier to replace all matches, rather than only the first one.

/g 启用“全局”匹配。使用 replace() 方法时,指定此修饰符以替换所有匹配项,而不仅仅是第一个匹配项。

回答by sushil bharwani

You need to look for some replaceAll option

您需要寻找一些 replaceAll 选项

str = str.replace(/ /g, "+");

this is a regular expression way of doing a replaceAll.

这是执行replaceAll 的正则表达式方式。

function ReplaceAll(Source, stringToFind, stringToReplace) {
    var temp = Source;
    var index = temp.indexOf(stringToFind);

    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
    }

    return temp;
}

String.prototype.ReplaceAll = function (stringToFind, stringToReplace) {
    var temp = this;
    var index = temp.indexOf(stringToFind);

    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
    }

    return temp;

};

回答by Motlicek Petr

NON BREAKING SPACE ISSUE

非突破性空间问题

In some browsers

在某些浏览器中

(MSIE "as usually" ;-))

(MSIE“像往常一样”;-))

replacing space in string ignores the non-breaking space (the 160 char code).

替换字符串中的空格会忽略不间断空格(160 个字符代码)。

One should always replace like this:

人们应该总是像这样替换:

myString.replace(/[ \u00A0]/, myReplaceString)

Very nice detailed explanation:

非常好的详细解释:

http://www.adamkoch.com/2009/07/25/white-space-and-character-160/

http://www.adamkoch.com/2009/07/25/white-space-and-character-160/

回答by Rick

Do this recursively:

递归地执行此操作:

public String replaceSpace(String s){
    if (s.length() < 2) {
        if(s.equals(" "))
            return "+";
        else
            return s;
    }
    if (s.charAt(0) == ' ')
        return "+" + replaceSpace(s.substring(1));
    else
        return s.substring(0, 1) + replaceSpace(s.substring(1));
}