Created
December 10, 2012 02:39
-
-
Save jeffreyiacono/4248059 to your computer and use it in GitHub Desktop.
extend js String to implement basic trim methods
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 characters
| function strltrim() { | |
| return this.replace(/^\s+/,''); | |
| } | |
| function strrtrim() { | |
| return this.replace(/\s+$/,''); | |
| } | |
| function strtrim() { | |
| return this.replace(/^\s+/,'').replace(/\s+$/,''); | |
| } | |
| String.prototype.ltrim = strltrim; | |
| String.prototype.rtrim = strrtrim; | |
| String.prototype.trim = strtrim; | |
| var name = " middle "; | |
| console.log("a" + name.trim() + "z"); // amiddlez | |
| console.log("a" + name.rtrim() + "z"); // a middlez | |
| console.log("a" + name.ltrim() + "z"); // amiddle z |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment