How to replace all spaces in a string

JavascriptJqueryStringReplace

Javascript Problem Overview


I have two html text input, out of that what ever user type in first text box that need to reflect on second text box while reflecting it should replace all spaces to semicolon. I did to some extant and it replacing for first space not for all, I think I need to use .each function of Jquery, I have used .each function but I didn't get the result see this

HTML :

Title : <input type="text" id="title"><br/>
Keyword : <input type="text" id="keyword">

Jquery:

$('#title').keyup(function() {
    var replaceSpace = $(this).val(); 

        var result = replaceSpace.replace(" ", ";");
        
        $("#keyword").val(result);
 
});

Thanks.

Javascript Solutions


Solution 1 - Javascript

var result = replaceSpace.replace(/ /g, ";");

Here, / /g is a regex (regular expression). The flag g means global. It causes all matches to be replaced.

Solution 2 - Javascript

Pure Javascript, without regular expression:

var result = replaceSpacesText.split(" ").join("");

Solution 3 - Javascript

takes care of multiple white spaces and replaces it for a single character

myString.replace(/\s+/g, "-")

http://jsfiddle.net/aC5ZW/340/

Solution 4 - Javascript

VERY EASY:

just use this to replace all white spaces with -:

myString.replace(/ /g,"-")

Solution 5 - Javascript

Simple code for replace all spaces

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

Out put: Howareyou

Solution 6 - Javascript

    $('#title').keyup(function () {
        var replaceSpace = $(this).val();

        var result = replaceSpace.replace(/\s/g, ";");

        $("#keyword").val(result);

    });

Since the javascript replace function do not replace 'all', we can make use the regular expression for replacement. As per your need we have to replace all space ie the \s in your string globally. The g character after the regular expressions represents the global replacement. The seond parameter will be the replacement character ie the semicolon.

Solution 7 - Javascript

I came across this as well, for me this has worked (covers most browsers):

myString.replace(/[\s\uFEFF\xA0]/g, ';');

Inspired by this trim polyfill after hitting some bumps: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim#Polyfill

Solution 8 - Javascript

You can do the following fix for removing Whitespaces with trim and @ symbol:

var result = string.replace(/ /g, '');  // Remove whitespaces with trimmed value
var result = string.replace(/ /g, '@'); // Remove whitespaces with *@* symbol

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRajesh HatwarView Question on Stackoverflow
Solution 1 - JavascriptPaul DraperView Answer on Stackoverflow
Solution 2 - JavascriptZonView Answer on Stackoverflow
Solution 3 - JavascriptricksView Answer on Stackoverflow
Solution 4 - JavascriptCommonSenseCodeView Answer on Stackoverflow
Solution 5 - JavascriptArshid KVView Answer on Stackoverflow
Solution 6 - JavascriptJobelleView Answer on Stackoverflow
Solution 7 - JavascriptstefanView Answer on Stackoverflow
Solution 8 - JavascriptPushkarView Answer on Stackoverflow