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

adrianbanks
- 81,306
- 22
- 176
- 206

Pablo.K
- 1,041
- 2
- 10
- 15
-
2It means `_` since it is in a char set. – Daniel A. White Jan 03 '16 at 01:26
-
Uhh, the `_` is the `_` character (which is a word character). – paulotorrens Jan 03 '16 at 01:26
-
http://regexper.com/#%2F%5B%5CW_%5D%2Fg – Daniel A. White Jan 03 '16 at 01:26
-
It's a character set. `\W` is equivalent to `[^a-zA-Z0-9_]`, (i.e., not (`a-zA-Z0-9_`) , and `_` will match `_`. – Josh Crozier Jan 03 '16 at 01:26
1 Answers
0
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

Joseph Didion
- 142
- 1
- 6