Replace all spaces in a string with '+'

javascript, string

Solution

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

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

Problem

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?

Original source

Related problems