Forked from DBJDBJ/cross_browser_cross_javascript_string_trim.js
Created
March 11, 2010 16:01
Revisions
-
DBJDBJ created this gist
Mar 11, 2010 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ // dbjdbj@gmail.com // Based on work by http://github.com/dieseltravis (function () { var trimLeft = /^\s+/, trimRight = /\s+$/; // Verify that \s matches non-breaking spaces (IE fails on this test) if ( !/\s/.test( "\xA0" ) ) { trimLeft = /^[\s\xA0]+/; trimRight = /[\s\xA0]+$/; } // Use native String.prototype.trim function wherever possible // Do not use String.trim. It is not part of ES5 window.dbj_trim = "function" === typeof "".trim ? function( text ) { return ! text ? "" : String.prototype.trim.call( text.toString() ); } : // Otherwise use our own trimming functionality function( text ) { return ! text ? "" : text.toString().replace( trimLeft, "" ).replace( trimRight, "" ); } }());