I have the following code:
var splitter = (filterValue.length > 0 ? filterValue.match(/^\s*(<|>)?\s*(\d\d\/\d\d\/\d{4})\s*\-?\s*(\d\d\/\d\d\/\d{4})?\s*$/).slice(1) : '');
Where filterValue
could be:
01/01/2013
> 01/01/2013
< 01/01/2013
01/01/2013 - 01/01/2013
And then I split these into chunks like:
var string1 = (splitter.length > 0 ? splitter[0] : '');
var string2 = (splitter.length >= 2 ? splitter[1] : '');
var string3 = (splitter.length >= 3 ? splitter[2] : '');
var string4 = (splitter.length < 4 ? splitter[3] : '');
string1
would be either '', '>', '<', ''
for the above examples.
string2
would always be the first date
string3
would be either ''
or '-'
if the dash is present between two dates
string4
would be the last date.
However I can't get the last scenario to work. So presume I have a problem in my regex for handling this. Can anyone see the problem?
Fiddle: http://jsfiddle.net/7LR49/1/
I'm getting:
undefined
01/01/2013
01/01/2013
undefined
When I should be getting:
undefined
01/01/2013
-
01/01/2013
and doing a console on splitter shows:
[undefined, "01/01/2013", "01/01/2013"]
So the last 3rd key isn't actual being picked up by the regex. And instead getting the 4th in its place.