I would like to know if there is a better regex to match all worded numbers from 1 to 99.
With "better" I mean shorter with same performance, OR faster.
I came up with this. I can't do better.
const regex = /\b(?<!\-)(?:(?:twen(?=ty)|thir|four(?=te)|for(?=ty)|fif|six|seven|eigh|nine)t(?:een|y)(?:(?<=ty)\-(?:one|two|three|four|five|six|seven|eight|nine))?|(?:one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve))/g;
const testString = see_regex101_link
const isMatch = regex.test(testString);
console.log(isMatch);
You can test it here: https://regex101.com/r/CYBK5S/1 Javascript if possible.
suffixes = ["one", "two", ..., "nine"]
,prefixes = ["twenty", "thirty", ..., "ninety"]
,teens = ["ten", "eleven", ..., "nineteen"]
,cartesian_product = prefixes.product(['-'], suffices).map(&:join) #=> ["twenty-one", "twenty-two", ..., "twenty-nine", "thirty-one", ..., "ninety-nine"]
, then create a constant that holds a set of 99 strings:ONE_TO_ONE_HUNDRED = Set[suffices + teens + prefixes + cartesian_product]
. \$\endgroup\$