Skip to content

Instantly share code, notes, and snippets.

@Williammer
Created June 17, 2014 09:29
Show Gist options
  • Select an option

  • Save Williammer/e50ce97b75a2a0378bdc to your computer and use it in GitHub Desktop.

Select an option

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.
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