Trim space in javascript

var myString = "                  some text                  ";

alert(myString.trim());

In IE first use this and call like the example above.

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

Means: matching anything begin with \s (“^”) AND anything end with \s ($), globally.

from here http://goo.gl/L802W

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s