String.prototype.nospace = function(){ return this.replace(/\s/g,"") } var str = " hello world "; console.log(str.nospace()); //helloworld