Question

http://jsperf.com/regex-not-word-selector

Why do /\W/g performs worse than /[^A-Za-z]/g?

Is it JS specific?

Was it helpful?

Solution

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

\w is equivalent to [A-Za-z0-9_]

and

\^W is equivalent to [^A-Za-z0-9_]

Note that in implementations in other languages \w may or may not also match unicode characters.

The results are pretty close and in no way something I would worry about.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top