最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How To Remove Multiple Spaces from String jQuery - Stack Overflow

matteradmin3PV0评论

I have an excel spreadsheet that has 3 columns.

    1st        |    2nd    |    3rd 
----------------------------------------
  xxxxxxxx           x        xxxxxxxx

When I copy all 3 of those cells and paste them into my textbox I get a value that looks likes this:

I need to eliminate the spaces, but what I have researched isn't working.

Here is what I have:

$(document).ready(function() {
    $("#MyTextBox").blur(function() {
        var myValue = $(this).val();
        alert(myValue);
        var test = myValue.replace(' ', '');
        alert(test);
        $("#MyTextBox").val(test);

    });
});

When I alert test it looks the exact same as the original and the value for MyTextBox isn't being replaced.

I have a JSFiddle where I'm trying to replicate the issue, but in this instance, only the 1st space is replaced but the new value is populating into the textbox.

What am I doing wrong?

Any help is appreciated.

I have an excel spreadsheet that has 3 columns.

    1st        |    2nd    |    3rd 
----------------------------------------
  xxxxxxxx           x        xxxxxxxx

When I copy all 3 of those cells and paste them into my textbox I get a value that looks likes this:

I need to eliminate the spaces, but what I have researched isn't working.

Here is what I have:

$(document).ready(function() {
    $("#MyTextBox").blur(function() {
        var myValue = $(this).val();
        alert(myValue);
        var test = myValue.replace(' ', '');
        alert(test);
        $("#MyTextBox").val(test);

    });
});

When I alert test it looks the exact same as the original and the value for MyTextBox isn't being replaced.

I have a JSFiddle where I'm trying to replicate the issue, but in this instance, only the 1st space is replaced but the new value is populating into the textbox.

What am I doing wrong?

Any help is appreciated.

Share Improve this question edited Nov 15, 2017 at 15:07 Grizzly asked Nov 15, 2017 at 15:04 GrizzlyGrizzly 5,9539 gold badges61 silver badges115 bronze badges 4
  • 1 is this what you want jsfiddle/n0oot23g/1 ? .replace(/ +/g, ' ') will replace multiple spaces with only 1 – Carsten Løvbo Andersen Commented Nov 15, 2017 at 15:07
  • you need a regex, try: myValue.replace(/\s/g, ''). \s is for space character nad g will replace all spaces – n4m31ess_c0d3r Commented Nov 15, 2017 at 15:08
  • @CarstenLøvboAndersen Thank you. I didn't even think to use regular expressions! Thank you! – Grizzly Commented Nov 15, 2017 at 15:14
  • This link may help you : stackoverflow./questions/1981349/… – Meg Commented Nov 15, 2017 at 15:55
Add a ment  | 

2 Answers 2

Reset to default 11

I've changed your replace with a regex. This removes all the spaces


$("#MyTextBox").blur(function(){
    var myValue = $(this).val();
    alert(myValue);
    var test = myValue.replace(/\s/g, '');
    alert(test);
    $("#MyTextBox").val(test);
});

Using a regular expression would replace any number of spaces.

$(document).ready(function() {
    var str = 'Your string';
    var stringWithoutSpace = str.replace(/\s/g, '')
});
Post a comment

comment list (0)

  1. No comments so far