71

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.

2

8 Answers 8

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

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

Sign up to request clarification or add additional context in comments.

9 Comments

Also worth noting: use / +/g to replace runs of spaces with only one semicolon. Use /\s/g to replace all white space characters. Use /\s+/g to replace all runs of white spaces.
Or you could do this: $("#keyword").val($(this).val().split(' ').join(';')); if you wanted (for whatever reason) to avoid regexes
@Kay's edit (jsfiddle.net/pLmv7) is likely the best approach, it easily deals with multiple spaces producing only a single semicolon
And probably you what to use replaceSpace.trim() before applying the replace. trim() strips off leading and trailing white spaces.
@JasonSperske, you could change it to /\s+(?=\S)/g. (Though /(?<=\S)\s+(?=\S)/g works for beginning and end.) There now you can like regexes again.
|
22

Pure Javascript, without regular expression:

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

2 Comments

any difference in speed compared to the regex?
Tested - no performance difference, at least worthing attention.
9

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

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

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

Comments

6

VERY EASY:

just use this to replace all white spaces with -:

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

1 Comment

How is this any different than the accepted answer 4 years prior in 2013?
3

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

Comments

2

Simple code for replace all spaces

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

Out put: Howareyou

1 Comment

How is this any different than the same answer 2 months prior?
2
    $('#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.

2 Comments

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
@badacadabra upadated :)
-1

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

1 Comment

Please describe in your answer, what was the problem, and how will this snippet solve it, to help others understand this answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.