This question already has an answer here:
这个问题已经有了答案:
- How to replace all occurrences of a string in JavaScript? 41 answers
- 如何在JavaScript中替换字符串的所有出现?41岁的答案
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?
但它只取代了第一次出现。我怎样才能让它取代所有的事件呢?
9 个解决方案
#1
335
Here's an alternative that doesn't require regex:
这里有一个不需要regex的替代方案:
var str = 'a b c';
var replaced = str.split(' ').join('+');
#2
365
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在默认情况下只替代第一次出现。
#3
67
var str = 'a b c';
var replaced = str.replace(/\s/g, '+');
#4
36
You can also do it like
你也可以这样做。
str = str.replace(/\s/g, "+");
Have a look to the fiddle: http://jsfiddle.net/aC5ZW/
看一下小提琴:http://jsfiddle.net/aC5ZW/ ?
#5
20
Use global search in the string. g flag
在字符串中使用全局搜索。g标志
str.replace(/\s+/g, '+');
source: replaceAll function
来源:replaceAll函数
#6
18
Use a regular expression with the g
modifier:
使用正则表达式与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()方法时,指定这个修改器来替换所有匹配项,而不是仅替换第一个匹配项。
#7
4
You need to look for some replaceAll option
你需要找一些替代品。
str = str.replace(/ /g, "+");
this is a regular expression way of doing a 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;
};
#8
4
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/
#9
-1
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));
}
#1
335
Here's an alternative that doesn't require regex:
这里有一个不需要regex的替代方案:
var str = 'a b c';
var replaced = str.split(' ').join('+');
#2
365
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在默认情况下只替代第一次出现。
#3
67
var str = 'a b c';
var replaced = str.replace(/\s/g, '+');
#4
36
You can also do it like
你也可以这样做。
str = str.replace(/\s/g, "+");
Have a look to the fiddle: http://jsfiddle.net/aC5ZW/
看一下小提琴:http://jsfiddle.net/aC5ZW/ ?
#5
20
Use global search in the string. g flag
在字符串中使用全局搜索。g标志
str.replace(/\s+/g, '+');
source: replaceAll function
来源:replaceAll函数
#6
18
Use a regular expression with the g
modifier:
使用正则表达式与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()方法时,指定这个修改器来替换所有匹配项,而不是仅替换第一个匹配项。
#7
4
You need to look for some replaceAll option
你需要找一些替代品。
str = str.replace(/ /g, "+");
this is a regular expression way of doing a 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;
};
#8
4
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/
#9
-1
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));
}