Reputation: 1606
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?
Upvotes: 1
Views: 65
Reputation: 191729
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)
Upvotes: 3
Reputation: 784998
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
Upvotes: 1
Reputation: 1405
try substring
String str="20130820";
String year=str.subString(0,3);
String month=str.subString(4,5);
String date=Str.subString(6,7);
Upvotes: 0