I am new to regex, and have looked all over, though I could not find an exact regex that works. There are no whitespaces in the string, and the dates can be surrounded by any random text, non-date, characters.
Sample Strings and expected responses:
EX 1:
From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember_currency_CNY
var res = ['2017-01-01', '2017-12-31']
EX 2:
From2016-01-01to2016-12-31
var res = ['2016-01-01', '2016-12-31']
EX 3:
From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember
var res = ['2017-01-01', '2017-12-31']
EX 4:
AsOf2017-12-31
var res = ['2017-12-31']
I have teted this regex (among others), with no avail:
/\b\d{4}-\d{2}-\d{2}\b/g
I was using this tool to test: http://rubular.com/r/bce4IHyCjW
2017-02-29(February 29th, but 2017 isn't a leap year) and2017-04-31(April 31st, but April has 30 days), not to mention cases like9999-99-99. Actually, with regex, you'll get a list of possible dates (things that look like dates), but you'll have to validate them later if you want to make sure they're really valid dates. Of course you can ignore this if you can guarantee that the inputs always have valid dates.