2

I have a WordPress site, and am using AJAX to update the archive rather than page reloads (in very basic terms).

So I can either be passing:

http://mysite.com/events/2012/10/17
http://mysite.com/events/2012/10
http://mysite.com/events/2012

I am looking for a JS/jQuery regex method that will retrieve any of these. So far I have the following:

var linkUrl = 'http://mysite.com/events/2012/10';
var linkDate = linkUrl.match(/\d{4}(\/\d{2})+/);
console.log( linkDate ); // output - ["2012/10", "/10", index: 8, input: "/events/2012/10"] 

It appears that it's finding 2 matches, the second of which is not what I want. I'm sure it's a simple thing in my regex.

7
  • This is clearly example code. In your actual code, is the url in an <a> element that's being clicked? Commented Jun 26, 2013 at 13:20
  • 1
    Did you check this question: stackoverflow.com/questions/4478426/… ? Commented Jun 26, 2013 at 13:25
  • @CrazyTrain yes this is example code, the linkUrl is being populated by $(this).attr('href') which is essentially giving me what I have displayed above. Commented Jun 26, 2013 at 13:27
  • @JonathanNaguin I didn't, and it is definitely helpful, however this expression is a little more advanced than /\d{4}\/\d{2}\/\d{2}/ as that ALWAYS looks for day only, not monthly. I discovered the solution - posted below. Commented Jun 26, 2013 at 13:32
  • Why do you care how many matches it returns? Just use linkDate[0]. Commented Jun 26, 2013 at 13:33

3 Answers 3

2

I would try this regexp /(\d{4})(?:\/(\d{2}))?(?:\/(\d{2}))?$/:

'http://mysite.com/events/2012'.match(/(\d{4})(?:\/(\d{2}))?(?:\/(\d{2}))?$/)
// => ["2012", "2012", undefined, undefined]

'http://mysite.com/events/2012/03'.match(/(\d{4})(?:\/(\d{2}))?(?:\/(\d{2}))?$/)
// => ["2012/03", "2012", "03", undefined]

'http://mysite.com/events/2012/03/21'.match(/(\d{4})(?:\/(\d{2}))?(?:\/(\d{2}))?$/)
// => ["2012/03/21", "2012", "03", "21"]
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the reply, @dfsq. Yours would work, but it looks kind of messy to me (too many brackets). see my answer for the most efficient method.
@EricHolmes: your "too many brackets" sounds just like the "too many notes" anecdote. Believe it or not, every one of them is necessary.
@thg435 Actually, they arent. See how his results have 03 and 21, and undefined ? That is because the nested brackets (while not necessary) are creating a sub-pattern capture that is not being ignored. Mine is cleaner, more efficient, and properly blocking out subpatterns.
Maybe I misunderstood, but I can't see how your regexp can possibly help: 'http://mysite.com/events/2012'.match(/\d{4}(?:\/\d{2})+/) => null. 'http://mysite.com/events/2012/02'.match(/\d{4}(?:\/\d{2})+/) => ["2012/02"]. To me it does not look very helpful. But as long as it works for you, great!
Good point - my situation always deals with at LEAST monthly archives. It should be this - /\d{4}(?:\/\d{2})*/. (updating answer)
0

Try this.

(?<=/)[0-9]{4}(/[0-9]{1,2})?(/[0-9]{1,2})?

Comments

0

So I have discovered the solution within my lovely O'Reilly Regular Expression book.

after looking at my regex again, I realized that the month[and day] expression is surrounded by brackets, which apparently means to capture the subpattern. The solution was this.

var linkUrl = "/events/2012/10/25";
var linkDate = linkUrl.match(/\d{4}(?:\/\d{2})*/);
// output - ["2012/10/25", index: 8, input: "/events/2012/10/25"] 

I added ?: within the brackets, which tells match to not capture the substring. Now I receive 2012/10/17 or 2012/10 respectvely, depending on the URL.

Also notice the single value being returned, due to properly excluding subpatterns.

Comments

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.