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
Selected Reading
d vs D in JavaScript?
In JavaScript regular expressions, \d and \D are metacharacters used to match different types of characters in strings. Understanding their differences is essential for effective pattern matching.
\d matches any single digit character (equivalent to [0-9]), while \D matches any character that is NOT a digit (equivalent to [^0-9]). These metacharacters are complete opposites of each other.
Syntax
Both metacharacters can be used in two ways:
// Using RegExp constructor
new RegExp("\d", "g") // matches digits
new RegExp("\D", "g") // matches non-digits
// Using regex literal
/\d/g // matches digits
/\D/g // matches non-digits
Example 1: Using \d to Match Digits
<!DOCTYPE html>
<html>
<head>
<title>\d in JavaScript regex</title>
</head>
<body>
<h3>\d Matches All Digits</h3>
<p id='result1'></p>
<script>
var string = "1012 Brewery House, Road no. 24 Gachibowli, Hyderabad!";
var reg_ex = new RegExp("\d", "g");
var output = string.match(reg_ex);
document.getElementById('result1').innerHTML = "Matched digits: " + output.join(", ");
</script>
</body>
</html>
Matched digits: 1, 0, 1, 2, 2, 4
Example 2: Using \D to Match Non-Digits
<!DOCTYPE html>
<html>
<head>
<title>\D in JavaScript regex</title>
</head>
<body>
<h3>\D Matches All Non-Digits</h3>
<p id='result2'></p>
<script>
var string = "Room 123A";
var reg_ex = new RegExp("\D", "g");
var output = string.match(reg_ex);
document.getElementById('result2').innerHTML = "Non-digit characters: " + output.join("");
</script>
</body>
</html>
Non-digit characters: Room A
Comparison: \d vs \D vs Custom Patterns
<!DOCTYPE html>
<html>
<head>
<title>\d vs \D Comparison</title>
</head>
<body>
<h3>Comparing \d and \D with Custom Patterns</h3>
<p id='result3'></p>
<script>
var string = "ABC123XYZ456";
// Using \d and its equivalent [0-9]
var digits1 = string.match(/\d/g);
var digits2 = string.match(/[0-9]/g);
// Using \D and its equivalent [^0-9]
var nonDigits1 = string.match(/\D/g);
var nonDigits2 = string.match(/[^0-9]/g);
var result = "\d matches: " + digits1.join("") + "<br>" +
"[0-9] matches: " + digits2.join("") + "<br>" +
"\D matches: " + nonDigits1.join("") + "<br>" +
"[^0-9] matches: " + nonDigits2.join("");
document.getElementById('result3').innerHTML = result;
</script>
</body>
</html>
\d matches: 123456 [0-9] matches: 123456 \D matches: ABCXYZ [^0-9] matches: ABCXYZ
Comparison Table
| Metacharacter | Equivalent Pattern | Matches | Example |
|---|---|---|---|
\d |
[0-9] |
Any digit (0-9) | "Hello123" ? ["1","2","3"] |
\D |
[^0-9] |
Any non-digit character | "Hello123" ? ["H","e","l","l","o"] |
Common Use Cases
- \d: Extracting phone numbers, validation of numeric input, parsing dates
- \D: Removing numbers from text, extracting alphabetic characters, text cleaning
Conclusion
\d and \D are opposite metacharacters in JavaScript regex. Use \d to match digits and \D to match everything except digits. They're equivalent to [0-9] and [^0-9] respectively.
Advertisements
