Question

I have a sting which contains a date, but date object wont accept it, so i have to make it into a valid format.

I tried this

"20130820".split(/^[a-z0-9]{4}[a-z]{2}[a-z0-9]{2}?$/) 

It should give out an array like

["2013", "08", "20"]

Any idea where i am wrong?

Was it helpful?

Solution

You want to use .match rather than .split. You need to capture each group, and the second character class is also a-z when it should probably just be \d.

"20130820".match(/^(\d{4})(\d{2})(\d{2})$/).slice(1)

OTHER TIPS

Why split, you can use String#match:

var m = "20130820".match(/^(\d{4})(\d{2})(\d{2})$/);
//=> ["20130820", "2013", "08", "20"]

btw for this simple job you don't need regex just use String#substring

try substring

   String str="20130820";
   String  year=str.subString(0,3);
   String  month=str.subString(4,5);
   String  date=Str.subString(6,7);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top