DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Javascript Trim

09.12.2005
| 380549 views |
  • submit to reddit
        Adds trim function to javascript string object.
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
    

Comments

Abdel Zoo replied on Wed, 2014/10/22 - 8:00pm

 toop topic brother  g9g العاب 

Snippets Manager replied on Sun, 2010/04/25 - 12:06pm

Thank a lot for the function

Snippets Manager replied on Fri, 2009/06/05 - 3:32am

if(!isBlank(ObjFrm.MCATXintCatId0,trimAll(ObjFrm.MCATXintCatId0.value),"Article Category")) return false; function trimAll(sString) { while (sString.substring(0,1) == ' ') { sString = sString.substring(1, sString.length); } while (sString.substring(sString.length-1, sString.length) == ' ') { sString = sString.substring(0,sString.length-1); } return sString; } function isBlank(obj,objval,msg) { if(objval == "") { //alert("- " + msg + " is required."); message = "- " + msg + " is required."; //message = "- " + msg; alert(message); if(obj.type != 'textarea') obj.focus(); return false; } else return true; } function isEmail(IsItReal,IsItRealval,name) { if (IsItRealval != "") { var valEmail = IsItRealval; var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; if(reg.test(valEmail)) return true; else { //alert('- Invalid '+name+'.') message = "- Invalid "+name+"."; //message = "- "+name; alert(message); IsItReal.focus(); return false; } } else { //alert('- '+name+' is required.') message = '- '+name+' is required.'; alert(message); IsItReal.focus(); return false; } }

Snippets Manager replied on Wed, 2008/06/25 - 7:05am

Sorry got the url wrong, here's the correct one: What is JavaScript trim?

Snippets Manager replied on Wed, 2008/06/25 - 7:05am

Definitely different ways of implementing this. Have a look at this article What is JavaScript trim?

Steven Levithan replied on Wed, 2008/01/30 - 1:58am

This post compares a variety of JavaScript trim implementations.

choonkeat chew replied on Sat, 2007/07/28 - 12:47am

missing "g" behind the regexp String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };

Snippets Manager replied on Wed, 2007/05/16 - 2:04pm

Sorry, that won't work either

Snippets Manager replied on Wed, 2007/05/16 - 2:04pm

you have to call it with str.trim NOT str.trim()

Snippets Manager replied on Thu, 2006/02/23 - 12:07pm

This should work... // Removes leading whitespaces function LTrim( value ) { var re = /\s*((\S+\s*)*)/; return value.replace(re, "$1"); } // Removes ending whitespaces function RTrim( value ) { var re = /((\s*\S+)*)\s*/; return value.replace(re, "$1"); } // Removes leading and ending whitespaces function trim( value ) { return LTrim(RTrim(value)); }

Snippets Manager replied on Tue, 2005/12/13 - 1:20am

The trim function isn't quite working properly eg. try: String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); }; function test() { var str = " a "; alert("Original string: '" + str + "'"); str = str.trim(); alert("Incompletely stripped string: '" + str + "'"); } ie. It doesn't strip all whitespace. This is because the regular expression should be: this.replace(/^\s+|\s+$/g, ""); eg. String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };