'from a string that contains a date range

when you born in 1995 you also can make a friend 2002 but also not exactly than 1788. The result should be [1995, 1788]



Solution 1:[1]

I'm not sure this solution is better than the one from @Ali but it's a different approach, where everything is done through the RegExp. I'll admit, the RegExp is a bit of a mouthful to cover edge cases...

const getDates = (text) => {
  const datePattern = new RegExp(/(?:^|[^\d])((?:19|20)\d{2})(?:$|[^\d])/, 'g');  
  const results = [];
  let match;
  while ((match = datePattern.exec(text)) !== null) {
    results.push(parseInt(match[1]));
  }
  return results;
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ben