Reputation: 14490
I have this date/2 word parsed from the database randomly:
JAN 2012
FEB 2013
MAR 2014
etc.
I want to make the month in variable a and the year in variable b. The date is parsed in a span as such:
<span class="rt_item">DEC 2012</span>
Upvotes: 2
Views: 1111
Reputation: 1531
Try this code:
var str="DEC 2012";
var mon=str.substr(0,3);
var year=str.substr(4,7);
Upvotes: 10
Reputation: 122906
You could create an array of objects containing a (month) and b (year) properties, derived from the span(s):
var md = [],
rtitems = $('.rt_item');
for (var i=0;i<rtitems.length;i+1){
var item = rtitems[i].text().split(' ');
md.push ({a: item[0], b: item[1]});
}
for a set of spans like:
<span class="rt_item">DEC 2012</span>
<span class="rt_item">JAN 2012</span>
<span class="rt_item">FEB 2013</span>
md[0].a
would be 'DEC'
, and md[0].b
2012
Upvotes: 4
Reputation: 19262
var mySplitResult = "MAR 2014".split(" ");
var x = mySplitResult[0];
var y = mySplitResult[1];
Upvotes: 6
Reputation: 67502
You can use regex for this:
var regex = /([A-Z]{3}) ([0-9]{4})/;
var matches = "JAN 2012".match(regex);
var a = matches[1];
var b = matches[2];
Otherwise, use .split(" ")
:
var matches = "JAN 2012".split(" ");
var a = matches[0];
var b = matches[1];
Both methods demonstrated in this fiddle.
Upvotes: 7