0

I have 8 possible URL structures that I need to check in my javascript code and then run a function if the current page meets the correct criteria.

There are 8 possible variations of the URL and all will include the keyword questions, so the first check needs to isolate and identify if the current page has the keyword 'questions' in its structure :-

  1. https://meta.stackexchange.com/questions

  2. https://meta.stackexchange.com/questions/ask

  3. https://meta.stackexchange.com/questions?sort=newest

  4. https://meta.stackexchange.com/questions?sort=featured

  5. https://meta.stackexchange.com/questions?sort=active

  6. https://meta.stackexchange.com/questions?sort=votes

  7. https://meta.stackexchange.com/questions?sort=hot

  8. https://meta.stackexchange.com/questions/1308/urgent-help-needed-i-screwed-up-my-site-really-bad-my-realty-questions

I am only interested in the latter (URL structure8) which will always have the keyword 'questions' in the url followed by a forward slash (/) and then a number from 1 to 9.

How can I write a js instr / regex function that can determine on page load if the page url matches the format of option 8 above.

Hope someone can help!

Thanks

Jonathan

3 Answers 3

1

Try this:

var str = "http://meta.stackexchange.com/questions/1308/urgent-help-needed-i-screwed-up-my-site-really-bad-my-realty-questions";
var regex = /^.*questions\/\d.*$/;
document.write(str.match(regex));
Sign up to request clarification or add additional context in comments.

1 Comment

thanks bart - that worked like a dream right out of the box! - thanks so much!
1

Are you interested only in urls or the type that contain questions followed by a slash(/) and then by any number or only 1-9? This one matches any number

regularex= /^http:\/\/meta.stackexchange.com\/questions\/[0-9]+\/.*$/;
check = url.match(regularex)

//check will be null if there is no match

Comments

0

I think you can try this:

var str="http://meta.stackexchange.com/questions/1308/urgent-help-needed-i-screwed-up-my-site-really-bad-my-realty-questions";
document.write(str.search('http://meta.stackexchange.com/questions/[0-9]+'));

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.