Last active
August 29, 2015 13:56
-
-
Save hunterc/8805952 to your computer and use it in GitHub Desktop.
JS String startsWith, endsWith 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
| var stringUtil = (function(global)) { | |
| // prototype modifiers | |
| if (typeof String.prototype.startsWith !== 'function') { | |
| String.prototype.startsWith = function(str) { | |
| return this.slice(0, str.length) === str; | |
| }; | |
| } | |
| if (typeof String.prototype.endsWith !== 'function') { | |
| String.prototype.endsWith = function(str) { | |
| return this.slice(-str.length) === str; | |
| }; | |
| } | |
| if (typeof String.prototype.stripTags !== 'function') { | |
| String.prototype.stripTags = function() { | |
| return this.replace(/(<([^>]+)>)/ig,""); | |
| } | |
| } | |
| /* | |
| add new functions here | |
| ex: | |
| var example = function(str) { | |
| // do something to str.... | |
| return str; | |
| }; | |
| */ | |
| /* | |
| expose new functions: key value pairing | |
| key => name used to call function in stringUtil | |
| value => private function declared inside stringUtil | |
| ex: | |
| return { | |
| ex: example | |
| }; | |
| */ | |
| return { | |
| }; | |
| })(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment