JavaScript RegExp \d Metacharacter
In JavaScript regular expressions, the \d metacharacter is used to match any digit character (0-9). This is a shorthand for the character class [0-9], which matches any single character within the specified range.
To begin, let’s look at a simple example where we use \d to match digits in a string.
let s = "The year is 2024 and the month is 12.";
let regex = /\d+/g;
let match = s.match(regex);
console.log(match);
Output
[ '2024', '12' ]
- The regular expression /\d+/g is used to match one or more digits globally in the string.
- The \d+ part ensures that we match one or more consecutive digits.
- The g flag indicates a global search, meaning it will find all matches in the string, not just the first one.
1. Match Exactly One Digit
You can also use \d to match a single digit in a string. Let’s see how to match only one digit.
let s = "The code 5 is valid, but 15 is too long.";
let regex = /\d/;
let match = s.match(regex);
console.log(match);
Output
[ '5', index: 9, input: 'The code 5 is valid, but 15 is too long.', groups: undefined ]
- The regular expression /\d/ will match the first digit it finds in the string.
- In this case, the match is “5”, which is the first digit in the string.
- Since we’re only looking for one digit, it stops after finding the first match.
2. Using \d with Anchors
You can combine the \d metacharacter with anchors like ^ (start of string) or $ (end of string) to match digits in specific positions in the string.
let s = "Year2024";
let regexS = /^\d+/;
let regexE = /\d+$/;
console.log(s.match(regexS));
console.log(s.match(regexE));
Output
null [ '2024', index: 4, input: 'Year2024', groups: undefined ]
- The regular expression /^\d+/ matches one or more digits at the start of the string.
- The regular expression /\d+$/ matches one or more digits at the end of the string.
- In both cases, the match is “2024”, as it appears at both the start and end of the string.
3. Matching Numbers with \d and Other Characters
You can use \d to match digits in more complex patterns, like when digits are surrounded by non-digit characters.
let s = "Order123 has been shipped.";
let regex = /\d+/;
let match = s.match(regex);
console.log(match);
Output
[ '123', index: 5, input: 'Order123 has been shipped.', groups: undefined ]
- The regular expression /\d+/ is used to match one or more digits in the string “Order123 has been shipped.”
- The match is “123”, which is found between the words “Order” and “has”.
4. Using \d for Validating Numbers
The \d metacharacter is frequently used for validating numerical patterns, such as checking if a string is a valid number or phone number.
let phone = "123-456-7890";
let regex = /^\d{3}-\d{3}-\d{4}$/;
let isValid = regex.test(phone);
console.log(isValid);
Output
true
- The regular expression /^\d{3}-\d{3}-\d{4}$/ ensures that the string matches a specific phone number format (XXX-XXX-XXXX).
- The \d{3} matches exactly three digits, and the – matches the hyphen between the digit groups.
- The ^ and $ anchors ensure that the pattern matches the entire string from start to end.
- The test() method checks if the phone number is in the correct format and returns true if it matches.
Key Points to Remember
- \d matches a single digit (equivalent to [0-9]).
- \d+ can match one or more digits.
- The g flag allows you to find all matches in a string.
- \d is commonly used for matching numbers, validating phone numbers, dates, or any other patterns that involve digits.
- Combine \d with anchors (^, $) to restrict the match to specific positions in the string (e.g., start or end).