6

Hi I'm trying to match a specific URL that allows for query strings. Basically I need the following to happen:

  • http://some.test.domain.com - Pass
  • http://some.test.domain.com/ - Pass
  • http://some.test.domain.com/home - Pass
  • http://some.test.domain.com/?id=999 - Pass
  • http://some.test.domain.com/home?id=888&rt=000 - Pass
  • http://some.test.domain.com/other - Fail
  • http://some.test.domain.com/another?id=999 - Fail

Here is what I have so far:

var pattern = new RegExp('^(https?:\/\/some\.test\.domain\.com(\/{0,1}|\/home{0,1}))$');
if (pattern.test(window.location.href)){
    console.log('yes');   
}

The above code only works for the first three and not for the query strings. Any help would be appreciated. Thanks.

3
  • 1
    Can you explain why those that pass pass and why those that fail fail? What is the logic here? Are only nothing and /home allowed? Commented Aug 29, 2013 at 20:53
  • @JamesMontagne I only want the first five conditions to work. Anything else should fail. Commented Aug 29, 2013 at 20:58
  • @progenhard Yes, I also need to get the top five links to pass. Commented Aug 29, 2013 at 21:03

2 Answers 2

9

A pattern like this should work (at least for your specific domain)

/^http:\/\/some\.test\.domain\.com(\/(home)?(\?.*)?)?$/

This will match a literal http://some.test.domain.com optionally followed by all of a literal /, optionally followed by a literal home, optionally followed by a literal ? and any number of other characters.

You can test it here

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

2 Comments

@progenhard It's not clear to me that OP cares about capturing groups, but sure, any of the groups could all be turned into non-capturing groups pretty easily (e.g. …(?:\/(?:home)?(?:\?.*)?)?$/)
The code uses pattern.test. I can't think of any reason it would matter if groups are capturing or not.
1

Don't use a Regex, use an URL parser. You could use purl

Then, you'll do:

url = "http://some.test.domain.com/home" // Or any other
purl(url).attr('path')  // is equal to "home" here.

You'll just need to check .attr('path') against your accepted paths (seemingly "", "/", and "home").


Here's some sample output:

purl("http://some.test.domain.com/?qs=1").attr('path')
"/"
purl("http://some.test.domain.com/other").attr("path")
"/other"
purl("http://some.test.domain.com/home").attr("path")
"/home"

4 Comments

@Qtax Haha thanks, I was thinking in Python for some reason : l
@ThomasOrozco Thanks for the input, but I'm limited to jQuery 1.9
@SamG.Daniel Keep in mind that Purl is just one file that's 267 lines of code. Its not like it's a massive library you're pulling in.
This is an excellent resource! thanks for turning me onto this instead of REGEX (we have a list of 25 allowed urls for filesharing sites) especially when added into jQuery.validator.addMethod(), IMHO it should have been included in jQuery.validator

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.