Checking Google Maps Links Using Regex

I just want to check if this link is a Google Maps link

For example:

var urls =[
    /// correct urls
    "https://www.google.com/maps",
    "https://www.google.fr/maps",
    "https://google.fr/maps",
    "http://google.fr/maps",
    "https://www.google.de/maps",
    "https://www.google.com/maps?ll=37.0625,-95.677068&spn=45.197878,93.076172&t=h&z=4",
    "https://www.google.de/maps?ll=37.0625,-95.677068&spn=45.197878,93.076172&t=h&z=4",
    "https://www.google.com/maps?ll=37.0625,-95.677068&spn=45.197878,93.076172&t=h&z=4&layer=t&lci=com.panoramio.all,com.google.webcams,weather",
    "https://www.google.com/maps?ll=37.370157,0.615234&spn=45.047033,93.076172&t=m&z=4&layer=t",

    "https://www.google.com/maps?ll=37.0625,-95.677068&spn=45.197878,93.076172&t=h&z=4",
    "https://www.google.de/maps?ll=37.0625,-95.677068&spn=45.197878,93.076172&t=h&z=4",
    "https://www.google.com/maps?ll=37.0625,-95.677068&spn=45.197878,93.076172&t=h&z=4&layer=t&lci=com.panoramio.all,com.google.webcams,weather",
    "https://www.google.com/maps?ll=37.370157,0.615234&spn=45.047033,93.076172&t=m&z=4&layer=t",

    /// error urls
    "https://www.google.com/",
    "https://zzz.google.com/maps",
    "https://www.google.com/+",
    "httpsxyz://www.google.com/maps",
    "http://www.anotherdomain.com/+"
    ];

      

I have such a bad attitude towards Regex and am trying to use JavaScript Regex Generator but it is still difficult for it.

I have only..

Reg = /^http\:\/\/|https\:\/\/|www\.google$/;

      

and its failure! :(

But I made a live test for all of you: http://jsbin.com/onuyux/1/edit?javascript,live


Edited: 2

After i got help from @joel harkes

and @dan1111

, i got regex

/^https?\:\/\/(www\.)?google\.[a-z]+\/maps\b/

      

This is a regex only for google.{TLD}/maps

, what about maps.google. {TLD}?

I just want to check google maps urls and urls from this path (look at the image)

enter image description here

If possible I want to check if the url is set or long-lat (using parameter validation li

or q

?) (Eg not only "maps.google.com") ..)

Updated list + code:

/// correct urls
"https://www.google.com/maps?ll=37.0625,-95.677068&spn=45.197878,93.076172&t=h&z=4",
"https://www.google.de/maps?ll=37.0625,-95.677068&spn=45.197878,93.076172&t=h&z=4",
"https://www.google.com/maps?ll=37.0625,-95.677068&spn=45.197878,93.076172&t=h&z=4&layer=t&lci=com.panoramio.all,com.google.webcams,weather",
"https://www.google.com/maps?ll=37.370157,0.615234&spn=45.047033,93.076172&t=m&z=4&layer=t",
"https://maps.google.com/maps?q=New+York,+NY,+USA&hl=no&sll=19.808054,-63.720703&sspn=54.337928,93.076172&oq=n&hnear=New+York&t=m&z=10",
"https://www.google.com/maps?q=New+York,+New+York,+USA&hl=no&ll=40.683762,-73.925629&spn=0.708146,1.454315&sll=41.47566,-72.026367&sspn=11.190693,23.269043&oq=new&hnear=New+York&t=m&z=10",

/// error urls
"https://www.google.com/",
"https://zzz.google.com/maps",
"https://www.google.com/+",
"httpsxyz://www.google.com/maps",
"http://www.anotherdomain.com/+",
"http://maps.google.com/",
"http://google.com/maps",
"http://google.de/maps",
"?q=New+York,+New+York,+USA&hl=no&ll=40.683762,-73.925629&spn=0.708146,1.454315&sll=41.47566,-72.026367&sspn=11.190693,23.269043&oq=new&hnear=New+York&t=m&z=10",
"&sll=41.47566,-72.026367&sspn=11.190693,23.269043&oq=new&hnear=New+York&t=m&z=10"

      

Live test: http://jsbin.com/onuyux/25/edit?javascript,live

+3


source to share


4 answers


Completing dan1111 responds here with a pattern that will match the specified domains and check if the ll GET variable is set:

Reg = /^https?\:\/\/(www\.)?google\.[a-z]+\/maps\/?\?([^&]+&)*(ll=-?[0-9]{1,2}\.[0-9]+,-?[0-9]{1,2}\.[0-9]+|q=[^&+])+($|&)/;

      

I haven't been able to test it (jsbin doesn't work) but it should match any url containing at least one of the ll or q parameters.

EDIT

I have added maps. subdomain and fixed a small typo. After q=

+

must be outside the character class for it to repeat: q=[^&]+

instead of q=[^&+]

. That being said, here's your regex:



Reg = /^https?\:\/\/(www\.|maps\.)?google\.[a-z]+\/maps\/?\?([^&]+&)*(ll=-?[0-9]{1,2}\.[0-9]+,-?[0-9]{1,2}\.[0-9]+|q=[^&]+)+($|&)/;

      

EDIT2

To accept google.co.uk style domains use the following regex.

Reg = /^https?\:\/\/(www\.|maps\.)?google(\.[a-z]+){1,2}\/maps\/?\?([^&]+&)*(ll=-?[0-9]{1,2}\.[0-9]+,-?[0-9]{1,2}\.[0-9]+|q=[^&]+)+($|&)/;

      

Hope it will cover all urls.

+2


source


(https|http)://(www\.|)google\.[a-z]+/maps

      

-> with a slash: /(https|http):\/\/(www\.|)google\.[a-z]+\/maps/

that the regex only selects good urls is not bad, but it doesn't check for enny \ GET variables



you can check: http://regexpal.com/ or here http://jsbin.com/onuyux/4

you can add ^ in front to check if the beginning of the line is

+2


source


Here's a simple regex that will work with your test data:

Reg = /^https?\:\/\/(www\.)?google\.(com|fr|de)\/maps\b/;

      

However, you should probably take a step back and think more about what you are trying to do. You have two problems here:

  • Make sure something is a valid URL (I suppose you need to do this because one of your examples is an invalid URL).
  • Make sure the URL is the Google Maps URL.

If you require a robust solution, I suggest doing it in a two-step process. Make sure the url is valid first and then see if it is a Google Maps url.

The first part is quite difficult on its own, but many have solved it before. If you search for "Javascript URL Validation" you will see many questions and many suggested answers. I suggest this as a place to start, but I have no personal experience with this in Javascript.

In the second part, you must define exactly what the Google Maps URL looks like. You've given a few examples above, but they are clearly a subset of the possibilities. How comprehensive are you? Do I need to validate the parameters? This can be a huge task.

+2


source


This should do the trick

Reg = /^https?\:\/\/((www|maps)\.)?google\.[a-z]+\/maps\/?\?([^&]+&)*(s?ll=-?[0-9]{1,2}\.[0-9]+,-?[0-9]{1,2}\.[0-9]+|q=[^&+])+($|&)/;

      

You can check it at http://jsbin.com/onuyux/36

+2


source







All Articles