What is this RegEx /[\W_]/g
in JavaScript? I know the \W
locates non-word characters, but what does the _
do?
Asked
Active
Viewed 96 times
-5
![](../../users/profiles/116923.webp)
adrianbanks
- 76,725
- 21
- 166
- 198
![](../../users/profiles/5414283.webp)
Pablo.K
- 761
- 1
- 7
- 13
-
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
![](../../users/profiles/5032276.webp)
Joseph Didion
- 121
- 4