Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
w vs W in JavaScript regex?
In JavaScript regex, \w and \W are complementary metacharacters used to match different types of characters. \w matches word characters (letters, digits, and underscores), while \W matches non-word characters (everything else).
Understanding \w Metacharacter
The \w metacharacter is equivalent to [a-zA-Z0-9_], matching any single letter (uppercase or lowercase), digit, or underscore.
Syntax
// Using RegExp constructor
RegExp("\w", "g")
// Using literal notation
/\w/g
Example: Using \w to Match Word Characters
<!DOCTYPE html>
<html>
<head>
<title>\w vs \W in JavaScript regex</title>
</head>
<body>
<h3>\w Metacharacter Example</h3>
<p id='result1'></p>
<script>
var string = "!#@ Tutorials point is one of the best e-learning platform @#!";
var reg_ex = new RegExp("\w", "g");
var output = string.match(reg_ex);
document.getElementById('result1').innerHTML = "Word characters found: " + output.join(", ");
</script>
</body>
</html>
Word characters found: T, u, t, o, r, i, a, l, s, p, o, i, n, t, i, s, o, n, e, o, f, t, h, e, b, e, s, t, e, l, e, a, r, n, i, n, g, p, l, a, t, f, o, r, m
Understanding \W Metacharacter
The \W metacharacter is equivalent to [^a-zA-Z0-9_], matching any character that is NOT a letter, digit, or underscore.
Syntax
// Using RegExp constructor
RegExp("\W", "g")
// Using literal notation
/\W/g
Example: Using \W to Match Non-Word Characters
<!DOCTYPE html>
<html>
<head>
<title>\w vs \W in JavaScript regex</title>
</head>
<body>
<h3>\W Metacharacter Example</h3>
<p id='result2'></p>
<script>
var string = "!#@ Tutorials point is one of the best e-learning platform @#!";
var reg_ex = new RegExp("\W", "g");
var output = string.match(reg_ex);
document.getElementById('result2').innerHTML = "Non-word characters found: " + output.join(", ");
</script>
</body>
</html>
Non-word characters found: !, #, @, , , , , , , , , -, , , , @, #, !
Comparison of \w and \W with Equivalent Patterns
<!DOCTYPE html>
<html>
<head>
<title>\w vs \W in JavaScript regex</title>
</head>
<body>
<h3>Comparing \w and \W with User-defined Patterns</h3>
<div id='result3'></div>
<script>
var string = "Hello123_World! @#$ Test";
var reg_w = /\w/g;
var reg_w_equivalent = /[a-zA-Z0-9_]/g;
var reg_W = /\W/g;
var reg_W_equivalent = /[^a-zA-Z0-9_]/g;
var w_matches = string.match(reg_w);
var w_equiv_matches = string.match(reg_w_equivalent);
var W_matches = string.match(reg_W);
var W_equiv_matches = string.match(reg_W_equivalent);
document.getElementById('result3').innerHTML =
'<strong>\w matches:</strong> ' + w_matches.join('') + '<br>' +
'<strong>[a-zA-Z0-9_] matches:</strong> ' + w_equiv_matches.join('') + '<br><br>' +
'<strong>\W matches:</strong> "' + W_matches.join('') + '"<br>' +
'<strong>[^a-zA-Z0-9_] matches:</strong> "' + W_equiv_matches.join('') + '"';
</script>
</body>
</html>
\w matches: Hello123_WorldTest [a-zA-Z0-9_] matches: Hello123_WorldTest \W matches: "! @#$ " [^a-zA-Z0-9_] matches: "! @#$ "
Key Differences
| Metacharacter | Equivalent Pattern | Matches | Examples |
|---|---|---|---|
\w |
[a-zA-Z0-9_] |
Word characters | a, Z, 5, _ |
\W |
[^a-zA-Z0-9_] |
Non-word characters | @, #, !, space |
Common Use Cases
Use \w to validate usernames, extract words, or filter alphanumeric content. Use \W to find special characters, punctuation, or clean unwanted symbols from text.
Conclusion
\w and \W are opposite regex metacharacters: \w matches letters, digits, and underscores, while \W matches everything else. They provide efficient ways to identify word and non-word characters in strings.
