68

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.

Rajesh Hatwar
  • 1,843
  • 6
  • 39
  • 58
  • 1
    possible duplicate of [Replacing spaces with underscores in JavaScript?](http://stackoverflow.com/questions/441018/replacing-spaces-with-underscores-in-javascript) – BuZZ-dEE Nov 24 '14 at 14:50
  • Does this answer your question? [How to replace all occurrences of a string?](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string) – HoldOffHunger Jul 31 '20 at 17:24

8 Answers8

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

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

Paul Draper
  • 78,542
  • 46
  • 206
  • 285
  • 25
    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. – Kijewski Nov 09 '13 at 06:14
  • 4
    Or you could do this: `$("#keyword").val($(this).val().split(' ').join(';'));` if you wanted (for whatever reason) to avoid regexes – Jason Sperske Nov 09 '13 at 06:16
  • 1
    @Kay's edit (http://jsfiddle.net/pLmv7/) is likely the best approach, it easily deals with multiple spaces producing only a single semicolon – Jason Sperske Nov 09 '13 at 06:18
  • And probably you what to use [`replaceSpace.trim()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim) before applying the replace. trim() strips off leading and trailing white spaces. – Kijewski Nov 09 '13 at 06:20
  • `/\s+\S/g` would prevent a semicolon from being added by trailing spaces – Jason Sperske Nov 09 '13 at 06:20
  • @JasonSperske `/\s+\S/g` drops the first character after a white space. – Kijewski Nov 09 '13 at 06:22
  • This is why I tend to avoid regexes :) – Jason Sperske Nov 09 '13 at 06:24
  • 3
    @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. – Paul Draper Nov 09 '13 at 06:30
  • I have come here like 100+ times lol – GaddMaster Oct 21 '21 at 11:19
22

Pure Javascript, without regular expression:

var result = replaceSpacesText.split(" ").join("");
Zon
  • 18,610
  • 7
  • 91
  • 99
9

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

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

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

ricks
  • 3,154
  • 31
  • 51
6

VERY EASY:

just use this to replace all white spaces with -:

myString.replace(/ /g,"-")
CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186
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.

Jobelle
  • 2,717
  • 1
  • 15
  • 26
2

Simple code for replace all spaces

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

Out put: Howareyou

Arshid KV
  • 9,631
  • 3
  • 35
  • 36
1

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

stefan
  • 2,685
  • 2
  • 24
  • 31
-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
Usman Khan
  • 3,739
  • 6
  • 41
  • 89
Pushkar
  • 1
  • 1
  • 1
    Please describe in your answer, what was the problem, and how will this snippet solve it, to help others understand this answer – FZs Nov 01 '19 at 07:16