Identify digits within a string

function extractDigits(text) {

var regex = /bd+b/g;

var matches = text.match(regex);

return matches ? matches : [];

}

// Usage

var text = “Order numbers: 123, 456 and 789.”;

var digits = extractDigits(text);

console.log(digits); // Output: [“123”, “456”, “789”]

Explanation

  • /bd+b/g: The regex pattern to match standalone digits.
  • b: Word boundary to ensure digits are not part of a larger word.
  • d+: One or more digits.
  • g: Global flag to find all matches in the string.
  • text.match(regex): Finds all matches of the regex in the given text.

Leave a comment

Your email address will not be published. Required fields are marked *