JavaScript RegExp [09] Expression (original) (raw)
Last Updated : 06 Dec, 2024
The **[0-9] expression in JavaScript regular expressions matches any single digit between 0 and 9. It is a character class used to represent a range of numeric characters.
JavaScript `
let regex = /[0-9]/g; let str = "abc123xyz"; let matches = str.match(regex); console.log(matches);
`
The pattern [0-9] matches the digits 1, 2, and 3 in the string.
Syntax:
/[0-9]/
- **[0-9]: Matches a single numeric character (0 through 9).
- **Modifiers (optional): Can be used with flags like g, i, or m for global, case-insensitive, or multiline matching.
Key Points
- Matches any digit from 0 to 9.
- Equivalent to the shorthand \d in JavaScript regex.
- Used in conjunction with quantifiers to match multiple digits or numeric patterns.
- Works within larger patterns for specific use cases, such as parsing numbers from strings.
Real-World Examples
1. Finding Digits in a String
JavaScript `
let regex = /[0-9]/g; let str = "Contact: 555-123-4567"; let matches = str.match(regex); console.log(matches);
`
Output
[ '5', '5', '5', '1', '2', '3', '4', '5', '6', '7' ]
2. Extracting Numbers
JavaScript `
let regex = /[0-9]+/g; // Matches one or more digits let str = "The order IDs are 123, 456, and 789."; let matches = str.match(regex); console.log(matches);
`
Output
[ '123', '456', '789' ]
The + quantifier ensures that consecutive digits are matched as a single number.
3. Validating Numeric Input
JavaScript `
let regex = /^[0-9]+$/; let input = "123456"; if (regex.test(input)) { console.log("Valid numeric input."); } else { console.log("Invalid input."); }
`
Output
Valid numeric input.
The pattern ^[0-9]+$ ensures the entire string consists only of digits.
4. Matching Numbers in a Date
JavaScript `
let regex = /[0-9]{4}/; // Matches exactly four digits let str = "The year is 2024."; let match = str.match(regex); console.log(match);
`
Output
[ '2024', index: 12, input: 'The year is 2024.', groups: undefined ]
Here, [0-9]{4} matches a four-digit sequence representing a year.
5. Replacing Digits
JavaScript `
let regex = /[0-9]/g; let str = "Room 101"; let result = str.replace(regex, "*"); console.log(result);
`
This example replaces all digits in the string with asterisks (*).
Common Patterns Using [0-9]
- **Match a Single Digit:
/[0-9]/
- **Match Multiple Digits:
/[0-9]+/
- **Match a Fixed Number of Digits:
/[0-9]{3}/ // Matches exactly 3 digits
- **Match Digits at the Start of a String:
/^[0-9]+/
- **Match Digits Within Non-Digit Characters:
/\b[0-9]+\b/
Comparison: [0-9] vs \d
- **[0-9]: Matches only numeric characters (0 to 9).
- **\d: Matches numeric characters, but may match additional Unicode digits depending on the environment.
For strict numeric matching, [0-9] is often preferred.
Why Use [0-9]?
- **Precision: Clearly specifies a range of numeric characters.
- **Versatility: Works seamlessly with other regex patterns and modifiers.
- **Control: Provides explicit control over matching rules, avoiding ambiguity.
Conclusion
The [0-9] expression is a fundamental building block in JavaScript regex, perfect for handling numeric data in strings.