0

I've NEVER been good with regular expressions despite my efforts. Can someone please help me out I'm trying to match urls in this format:

"/year/monthnum/day/postname/"

so:

"2012/05/03/whatever-the-text-here"

using:

if(window.location.pathname.match(...))
{
 //do something
}

I've tried a couple of things but I'm just terrible with regex... Can someone help me out here?

3 Answers 3

3

http://regexr.com?30ram

if(window.location.pathname.match(/\/?[0-9]{4}\/[0-9]{1,2}\/[0-9]{1,2}\/.+$/))

this can be enhanced also to check that day is not 0... or things like that... but i think its enough.

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome.. thanks very much Royi, one day I will find the time to spend a whole day getting to grips with regex but it's always been a minefield to me..
@Alex i'll be glad to help you with that. you can reference me with any question.
1

Try this:

if (/(?:(?:\d{4})[\\\/](?:\d{2})[\\\/](?:\d{2})[\\\/])([^\\\/]+)/m.test(subject)) {
    // Successful match
} else {
    // Match attempt failed
}

3 Comments

@stema:Right, but it could supports both the slashes, hence the effort!
hey Cylian, Thanks for your help, I've gone with the other simply because I understand whats going on better which is a little unfiar because I really dont understand regex too well but it will make it easier for me if I need to work with it
@Alex: You're welcome. You may visit here for a reference on RegEx.
0

may this url help you out more in URL regular expression

http://regexlib.com/Search.aspx?k=URL&AspxAutoDetectCookieSupport=1

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.