[javascript] Replacing spaces with underscores in JavaScript?

I'm trying to use this code to replace spaces with _, it works for the first space in the string but all the other instances of spaces remain unchanged. Anybody know why?

function updateKey()
{
    var key=$("#title").val();
    key=key.replace(" ","_");
    $("#url_key").val(key);
}

This question is related to javascript string

The answer is


Try .replace(/ /g,"_");

Edit: or .split(' ').join('_') if you have an aversion to REs

Edit: John Resig said:

If you're searching and replacing through a string with a static search and a static replace it's faster to perform the action with .split("match").join("replace") - which seems counter-intuitive but it manages to work that way in most modern browsers. (There are changes going in place to grossly improve the performance of .replace(/match/g, "replace") in the next version of Firefox - so the previous statement won't be the case for long.)


Just using replace:

_x000D_
_x000D_
var text = 'Hello World';_x000D_
    _x000D_
new_text = text.replace(' ', '_');_x000D_
    _x000D_
console.log(new_text);
_x000D_
_x000D_
_x000D_


_x000D_
_x000D_
const updateKey = key => console.log(key.split(' ').join('_'));
updateKey('Hello World');
_x000D_
_x000D_
_x000D_


I created JS performance test for it http://jsperf.com/split-and-join-vs-replace2


You can try this

 var str = 'hello     world  !!';
 str = str.replace(/\s+/g, '-');

It will even replace multiple spaces with single '-'.


I know this is old but I didn't see anyone mention extending the String prototype.

String.prototype.replaceAll = function(search, replace){
    if(!search || !replace){return this;} //if search entry or replace entry empty return the string
    return this.replace(new RegExp('[' + search + ']', 'g'), replace); //global RegEx search for all instances ("g") of your search entry and replace them all.
};

try this:

key=key.replace(/ /g,"_");

that'll do a global find/replace

javascript replace


Replace spaces with underscore

var str = 'How are you';
var replaced = str.split(' ').join('_');

Output: How_are_you


To answer Prasanna's question below:

How do you replace multiple spaces by single space in Javascript ?

You would use the same function replace with a different regular expression. The expression for whitespace is \s and the expression for "1 or more times" is + the plus sign, so you'd just replace Adam's answer with the following:

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

You can try this

 var str = 'hello     world  !!';
 str = str.replace(/\s+/g, '-');

It will even replace multiple spaces with single '-'.


_x000D_
_x000D_
const updateKey = key => console.log(key.split(' ').join('_'));
updateKey('Hello World');
_x000D_
_x000D_
_x000D_


try this:

key=key.replace(/ /g,"_");

that'll do a global find/replace

javascript replace


Just using replace:

_x000D_
_x000D_
var text = 'Hello World';_x000D_
    _x000D_
new_text = text.replace(' ', '_');_x000D_
    _x000D_
console.log(new_text);
_x000D_
_x000D_
_x000D_


Replace spaces with underscore

var str = 'How are you';
var replaced = str.split(' ').join('_');

Output: How_are_you


try this:

key=key.replace(/ /g,"_");

that'll do a global find/replace

javascript replace


I created JS performance test for it http://jsperf.com/split-and-join-vs-replace2


try this:

key=key.replace(/ /g,"_");

that'll do a global find/replace

javascript replace


To answer Prasanna's question below:

How do you replace multiple spaces by single space in Javascript ?

You would use the same function replace with a different regular expression. The expression for whitespace is \s and the expression for "1 or more times" is + the plus sign, so you'd just replace Adam's answer with the following:

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

I know this is old but I didn't see anyone mention extending the String prototype.

String.prototype.replaceAll = function(search, replace){
    if(!search || !replace){return this;} //if search entry or replace entry empty return the string
    return this.replace(new RegExp('[' + search + ']', 'g'), replace); //global RegEx search for all instances ("g") of your search entry and replace them all.
};