views:

54

answers:

2

Hi, my date formatting in PHP is d-M-Y and I'm trying to match the dates with a javascript regex:

s.match(new RegExp(/^(\d{1,2})(\-)(\w{3})(\-)(\d{4})$/))

To be used with the jQuery plugin, tablesorter. The problem is it's not working and I'm wondering why not.

I tried removing the dashes in my date() formatting (d M Y) and tried the ff and it worked:

s.match(new RegExp(/^\d{1,2}[ ]\w{3}[ ]\d{4}$/));

My question is what is the correct regex if I'm using dashes on PHP's date() i.e. d-M-Y? Thanks!

A: 

Try to replace it with either

s.match(new RegExp("^(\\d{1,2})(\\-)(\\w{3})(\\-)(\\d{4})$"));

or

s.match(/^(\d{1,2})(\-)(\w{3})(\-)(\d{4})$/);
shinkou
A: 

I'm usually more tolerant when matching dates, so I'd do something like this:

s.match( /^\s*(\d{1,2})\W+(\w{3})\W+(\d{4})\s*$/ )

(tolerate leading and trailing whitespace and any non-alphanumeric chars a s delimiters)

seanizer