JavaScript RegExp \D( non-digit characters) Metacharacter
Last Updated :
10 Dec, 2024
Improve
The RegExp \D Metacharacter in JavaScript is used to search non-digit characters i.e all the characters except digits. It is the same as [^0-9].
let str = "a1234g5g5";
let regex = /\D/g;
let match = str.match(regex);
console.log("Found " + match.length + " matches: " + match);
Output
Found 3 matches: a,g,g
Syntax:
/\D/
Example 1: Searches the non-digit characters in the whole string.
let str = "GeeksforGeeks@_123_$";
let regex = /\D/g;
let match = str.match(regex);
console.log("Found " + match.length + " matches: " + match);
Output
Found 17 matches: G,e,e,k,s,f,o,r,G,e,e,k,s,@,_,_,$
Example 2: Searches the non digit characters in the string.
let str = "Geeky@128";
let regex = new RegExp("\\D", "g");
let match = str.match(regex);
console.log("Found " + match.length
+ " matches: " + match);
Output
Found 6 matches: G,e,e,k,y,@
Difference Between \d and \D
Character Type | \d Matches | \D Matches |
---|---|---|
Digits (0-9) | Yes | No |
Digits (0-9) | No | Yes |
Symbols (@, #, $, etc.) | No | Yes |
Whitespace (spaces, tabs, newlines) | No | Yes |
Recommended Links:
- JavaScript RegExp Complete Reference
- JavaScript Cheat Sheet-A Basic guide to JavaScript
- JavaScript Tutorial