Created
October 20, 2011 21:49
-
-
Save ethertank/1302483 to your computer and use it in GitHub Desktop.
String.trim
This file contains 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 characters
(function() { | |
var _t = "[\x09-\x0D\x20\xA0\x85\u1680\u180E\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]+", | |
_l = new RegExp("^" + _t), | |
_r = new RegExp(_t + "$"), | |
SP = String.prototype; | |
SP.trimLeft = function() { return this.replace(_l, ""); }; | |
SP.trimRight = function() { return this.replace(_r, ""); }; | |
SP.trim = function() { return this.trimLeft().trimRight(); }; | |
})(); |
This file contains 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 characters
(function(P,s){var l=new RegExp("^"+s),r=new RegExp(s+"$");P.trimLeft=function(){return this.replace(l,"");};P.trimRight=function(){return this.replace(r,"");};P.trim=function(){return this.trimLeft().trimRight();};})(String.prototype,"[\x09-\x0D\x20\xA0\x85\u1680\u180E\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]+"); |
This file contains 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 characters
// usage : alert( " (・∀・) ".trim().length ); | |
// test | |
var s = "\x09\x0A\x0B\x0C\x0D" + | |
"\x20\xA0\x85\u1680\u180E" + | |
"\u2000\u2001\u2002\u2003\u2004\u2005" + | |
"\u2006\u2007\u2008\u2009\u200A" + | |
"\u2028\u2029" + | |
"\u202F\u205F\u3000\uFEFF"; | |
var echo = function(str){ document.write(str + "<br />"); }; | |
echo(s.length); | |
echo((s + "t" + s).length); | |
echo((s + "e" + s).trimLeft().length); | |
echo((s + "s" + s).trimRight().length); | |
echo((s + "t" + s).trim().length); | |
/* 変更 *//* | |
trimが実装されている場合でもネイティブメソッドを上書きし、実装毎の差異を強制的に無くす事にした。 | |
MLJ(http://www.mitsue.co.jp/knowledge/mjl.html)のソースコード内の丁寧なコメントにて気付く。 | |
ちなみに MLJはネイティブメソッドを上書きせず、同等のメソッドとして実装している。 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment