My code looks like follows
var html = $(el).html().toString();
html = '2012-05-10';
var re = new RegExp('^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$');
var m = re.exec(html);
if (m != null)
alert('match');
else
alert('nomatch');
The purpose of the regexp is to check whether a string contains any dates. However it never finds any dates. I have tested the regexp with an online regexp tool.
If you’re trying to find
whether a string contains any dates, then having^at the start and$isn’t going to help – as they denote to match the very start and end of the string.To find the date within a string that contains other text (or even whitespace), use this…
UPDATE
Anton has already figured out a solution – however, while the above solution is better than the original, it’s still not correct, as the
\dneeds to be escaped (to\\d) in order for it to work when contained within quotes. So instead it should be…However, in hindsight I agree with all others that as it’s not a dynamic pattern, the best solution is to use the
/pattern/syntax