Search code examples
javascriptregexjsperf

Regex for not word chars slower than excluding letter ranges


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

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

Is it JS specific?


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.