0

Not overly familiar with regex, I'm working with a custom calendar, and need to be able to parse out the month and year into separate variables.

To get my date, I am using kendo UI's month picker, so I just grab .val on change. Here is my code below, I've managed to get parse out the month, just need to create a regex that will parse out the year into a var called calYear:

$("#MonthPicker").change(function () {
    var x = $(this).val();
    console.log(x);
    var calMonth = x.replace(/\d+/g, ''); 
    console.log(calMonth);
});

Here is my console's output:

November 2013 <-- This is var x
November <-- This is var calMonth
4
  • 1
    show how the date will be Commented Jul 14, 2014 at 13:59
  • so, you want November? What's wrong with your code? Commented Jul 14, 2014 at 14:02
  • No, I just want the year. Commented Jul 14, 2014 at 14:02
  • 2
    Use split() with a space delimiter. This will put both in array indexes. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jul 14, 2014 at 14:05

2 Answers 2

3

You can just use String#split to get both values in single operation:

var tok = 'November 2013'.split(' ');
var month = tok[0]; // November
var year = tok[1]; // 2013
Sign up to request clarification or add additional context in comments.

Comments

2

By given info, you can use this:

var calMonth = x.match(/\d{4}$/g)[0];

3 Comments

Thanks, just what I was looking for. Give you the answer as soon as SO will let me.
@Mark glad to have helped :)
@Mark I had the idea of split, but you hadn't provided the example.. so, just use split() and you can choose the other answer as your answer..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.