Ad

\W RegEx JavaScript

- 1 answer

What is this RegEx /[\W_]/g in JavaScript? I know the \W locates non-word characters, but what does the _ do?

Ad

Answer

The whole regex string will match anything that is not letters and numbers

It is the same as [^a-zA-Z0-9]

The \W is included: (as you said) matches non word characters

the _ is included so it matches the literal underscore character that wouldn't be accepted in \W

Ad
source: stackoverflow.com
Ad