JAVASCRIPT
Extracting All Numerical Values from Text
Learn how to use a regular expression in JavaScript to efficiently extract all sequences of digits from a given string, useful for data cleaning and parsing.
const text = "Item ID: 123, Quantity: 50, Price: 19.99";
const numbers = text.match(/\d+/g);
console.log(numbers); // Output: ["123", "50", "19", "99"]
How it works: This snippet demonstrates using `String.prototype.match()` with a regular expression `/\d+/g` to find all occurrences of one or more digits. The `\d` metacharacter matches any digit (0-9), and `+` ensures one or more consecutive digits are matched. The `g` flag ensures all matches are returned in an array rather than just the first one.