1

I want to remove spaces on before of a strings and also replace all spaces between words to a space. If after string have spaces , please remove all spaces to a space. How can I do that?

Such as:

"       My           name   is    Eddy       " -> "My name is Eddy "

"       My           name   is    Eddy" -> "My name is Eddy"
4
  • Does trim is not work for you? Demo. Also refer Commented Feb 11, 2014 at 7:12
  • Why is the space after Eddy not removed in the first example? Commented Feb 11, 2014 at 7:13
  • @Bergi It's used for my auto complete. I just want to remove the duplicate query to server. Commented Feb 11, 2014 at 7:20
  • So you do not want to remove spaces after the strings? Please edit your question to fix that. Commented Feb 11, 2014 at 7:23

5 Answers 5

2

Here is what you want?

function removeSpace(string) {
    return string.replace(/\s*([^\s]+\s?)\s*/g, '$1');
}

The jsfiddle here: http://jsfiddle.net/bobkhin/5EPC8/

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

Comments

2

Replace multiple occurrences of spaces (\s+) with a single space ' ' with the String.replace method, then trim the remaining from the left and right with the String.trim method.

"       My           name   is    Eddy       ".replace(/\s+/g,' ').trim()

trim is available in FF 3.5+, IE 9+, Safari 9+.

Comments

1

Try this

var str=" My name is Eddy ";
alert(str.trim());

DEMO

Try this code also

if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}

3 Comments

@user3296015: Use a modern browser :-)
Please check my demo for better understanding
But I want it to work on IE6+ and it also doesnt work my cases.
1

You can use String.prototype.trim() on modern browsers (see compatibility):

" My name is Eddy ".trim()

will return

"My name is Eddy"

On IE8 you can use the polyfill suggested in the MDN page:

if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/gm, '');
  };
}

3 Comments

There's no trim() function in JS.
On my IE8, there's no trim function :(
I added a polyfill for IE8
1

There are many ways you can do this, but I find it easiest to understand with three individual replacements:

function cleanup( str ) {
    return str
        .replace( / +/g, ' ' )
        .replace( /^ /, '' )
        .replace( / $/, '' );
}

console.log(
    "'" +
    cleanup( "       My           name   is    Eddy       " ) +
    "'"
);

Logs:

'My name is Eddy'

This is compatible with very old browsers as well as new ones.

Edit: So you want to remove all leading spaces, but if there are trailing spaces you want to keep one trailing space, is that right? That's not how you described the problem.

If you look at the cleanup function I posted, it should be apparent how to fix it to not remove trailing spaces:

function cleanup( str ) {
    return str
        .replace( / +/g, ' ' )
        .replace( /^ /, '' );
}

console.log(
    "'" +
    cleanup( "       My           name   is    Eddy       " ) +
    "'"
);

Logs:

'My name is Eddy '

1 Comment

@user3296015: Just omit the third replacement

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.