0

I have this URL...

http://www.google.com/local/add/analytics?hl=en-US&gl=US

And I want to check these URLs to see if they matches above URL...

www.google.com/local/add*
www.google.com/local/add/*
http://www.google.com/local/add*
http://www.google.com/local/add/*
https://www.google.com/local/add*
https://www.google.com/local/add/*

You can see the input URL is also a regex having * so what regex that I can use to match a list of URLs with a regex to see if the url exists? Currently I am doing this...

var isAllowed = (url.indexOf(newURL) === 0);

Which is definitely not efficient.

2
  • 1
    Your question is not entirely clear. What is the purpose of the asterix (*)? does it mean a wildcard in the non-regex way? Commented Nov 29, 2011 at 10:32
  • @Variant I would assume the * is a wildcard yes.. and that the OP would like to validate the url against these 'cases'. Commented Nov 29, 2011 at 13:29

3 Answers 3

1

it's not the cleanest regex i've ever written but I think it should work.

var url = "http://www.google.com/local/add/analytics?hl=en-US&gl=US";
var reg = /((https|http|)(\:\/\/|)www\.google.com\/local\/add(\/|)).*/;
console.log(reg.test(url));

this will return true for all of these cases

www.google.com/local/add*
www.google.com/local/add/*
http://www.google.com/local/add*
http://www.google.com/local/add/*
https://www.google.com/local/add*
https://www.google.com/local/add/*

it should look for (http or https or nothing) then (:// or nothing) then www.google.com/local/add then (/ or nothing) then anything.

the one case it will also return true that I will leave for you is the case (http|https)www.google.com/local/add(/|)*

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

1 Comment

I do implore you to test the crap out of this if you are going to use it.. possibly read up a bit more on regex and improve on it (i'm no regex master myself... in fact i barley scrape by :P)
1
var reg = new RegExp("(https?://)?(www.)?google.com/local/add/?"),
    URL = "http://www.google.com/local/add/analytics?hl=en-US&gl=US";

console.log(reg.test(URL));

I've used the ? a lot, which means, whatever character precedes the question mark may or may not be matched.

https? means the s may or may not be there. (www.)? means that the www. may be absent entirely. You hopefully get how it works now.

Demo

Learn how to use Regular Expressions

1 Comment

ahhh. see I did not know that about ?. good show, good show.
0

As far as I understand you, you want something like this:

Convert the input URL to a regex. E.g.:

var input = "http://www.google.com/local/add*";
var reg_url = input .replace(/\*/g,".*").replace(/\./g,"\\."); 

you might need to escape some more characters, see here

And check if it matches:

var url = "http://www.google.com/local/add/analytics?hl=en-US&gl=US";
var isAllowed = url.search(reg_url) >= 0;

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.