Regex to Match All Numbers Except Those in the First Word

I am having trouble crafting a regex. For example, in the string A123 4HEL5P6 789 I want to match all the numbers 4, 5, 6, 7, 8, 9 but not 1, 2, 3.

I have tried using negative look behind with the regex (?<!^\w)\d+ but this matches the numbers in the first word.

Edit: Any numbers in the first continuous sequence of characters should not be matched, the first continuous sequence being from start (^) to a whitespace (\s). In 09B8A HE1LP only 1 should be matched, not 0, 9, or 8, as these digits are in the first word.

>Solution :

If your dialect supports variable-length negative lookbehinds, then this should work:

r = /(?<!^\w*)\d/g

console.log(...'A123 4HEL5P6 789'.match(r))

Otherwise, you could use /^\w*|\d/g and discard the first match.

Leave a Reply