Created
June 30, 2014 16:05
-
-
Save hehongwei44/3e167cfcda47d4c8051a to your computer and use it in GitHub Desktop.
对javascript中String类型的拓展
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
/** | |
* | |
* @desccrition: 对String类型去除空格的拓展 | |
* @dir : 被去除空格所在的位置 | |
* @test: ie6-9 chrome firefox | |
*/ | |
String.prototype.trim = function(dir){ | |
switch (dir) { | |
case 0 : //去左边的空格 | |
return this.replace(/(^\s*)/g,''); | |
break; | |
case 1 : //去右边的空格 | |
return this.replace(/(\s*$)/g,''); | |
break; | |
case 2 : //去掉所有的空格 | |
return this.replace(/(\s*)/g,''); | |
break; | |
default : //去掉两边的空格 | |
return this.replace(/(^\s*)|(\s*$)/g,''); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment