Created
June 17, 2014 09:29
-
-
Save Williammer/e50ce97b75a2a0378bdc to your computer and use it in GitHub Desktop.
jsRegExp.trim.js - implement the trim function of string with both regExp and string functions, while string functions has poor performance.
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
| String.prototype.trim = function () { | |
| var reg = /^\s+|\s+$/g, | |
| tr = function (full, hs, content, es) { | |
| alert('full: ' + full + '; hs: ' + hs + '; content: ' + content + '; es: ' + es); | |
| return content; | |
| }; | |
| return this.replace(reg, ''); | |
| }; | |
| //trim with string functions, with very poor performance. | |
| function trim(str) { | |
| var str = str.replace(/^\s\s*/, ''), | |
| ws = /\s/, | |
| i = str.length; | |
| while (ws.test(str.charAt(--i))); | |
| return str.slice(0, i + 1); | |
| } | |
| var case1 = " xx x_ "; | |
| var res = case1.trim(); | |
| console.log('res.length: ' + res.length + '; res: ' + res); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment