regexhq/whitespace-regex

Question about whitespace-regex

wudys opened this issue · 2 comments

wudys commented

Hello, sir.

In the course of my study, I found this repo.
I don't know if I can submit it up here, but I have a question.

In JavaScript or NodeJs, All of the values included in the regular expression are matched by \s.

I was wondering if you had any reason to write it like the code below.

module.exports = function regex() {
  return /^[\s\f\n\r\t\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff\x09\x0a\x0b\x0c\x0d\x20\xa0]+$/;
};

Here's the code I wrote.

'use strict';
// test: node v0.10.1

var pattern = /[\s]+/;
var testList = [
	" ", // 0 true
	"\f", // 1 true
	"\n", // 2 true
	"\r", // 3 true
	"\t", // 4 true
	"\u1680", // 5 true
	"\u180e", // 6 true
	"\u2000", // 7 true
	"\u2001", // 8 true
	"\u2002", // 9 true
	"\u2003", // 10 true
	"\u2004", // 11 true
	"\u2005", // 12 true
	"\u2006", // 13 true
	"\u2007", // 14 true
	"\u2008", // 15 true
	"\u2009", // 16 true
	"\u200a", // 17 true
	"\u2028", // 18 true
	"\u2029", // 19 true
	"\u202f", // 20 true
	"\u205f", // 21 true
	"\u3000", // 22 true
	"\ufeff", // 23 true
	"\x09", // 24 true
	"\x0a", // 25 true
	"\x0b", // 26 true
	"\x0c", // 27 true
	"\x0d", // 28 true
	"\x20", // 29 true
	"\xa0", // 30 true
	"isWhiteSpace" // 31 false
];

for (var i in testList) {
	console.log(i, testList[i].match(pattern) != null);
}

Thanks for reading my issue.
Have a nice day.

See https://github.com/niftylettuce/is-string-and-not-blank#benchmark benchmark, the for loop approach is 3-4x faster than a regex.