EN
JavaScript - what is \W regex / '\w' and '\W' difference ?
1 answers
3 points
What is \W
(/\W/
) regex and what is the difference between \w
and \W
?
1 answer
3 points
\w
- /\w/
- matches any word character (shortcut for [a-zA-Z_0-9]
).
\W
- /\W/
- matches any character that is not a word character (shortcut for [^\w]
).
Practical example that shows how to extract \w
and \W
characters from any string:
xxxxxxxxxx
1
const text = 'abc-123?';
2
3
const result1 = text.match(/\w/g);
4
const result2 = text.match(/\W/g);
5
6
if (result1) {
7
console.log(result1); // ['a', 'b', 'c', 1, 2, 3]
8
}
9
10
if (result2) {
11
console.log(result2); // ['-', '?']
12
}
0 commentsShow commentsAdd comment