用'+'替换字符串中的所有空格 [英] Replace all spaces in a string with '+'
本文介绍了用'+'替换字符串中的所有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含多个空格的字符串。我想用加号替换它们。我以为我可以使用
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?
推荐答案
这是一个不需要正则表达式的替代方案:
Here's an alternative that doesn't require regex:
var str = 'a b c';
var replaced = str.split(' ').join('+');
这篇关于用'+'替换字符串中的所有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文