Javascript Trim a String

How remove whitespace from both sides of a string (trim function):

Pure Javascript
var mystring = "       dotmaui.com        ";
mystring.trim();

For browsers that do not support the trim() method:

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

 

Leave a Comment

Your email address will not be published. Required fields are marked *