javascriptregexvalidationgoogle-mapsurl

Google Maps link validation using Regex


I just only want to check if that link is 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'm so bad about Regex and tried to use JavaScript Regex Generator but its still hard for me.

I got only ..

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

and its fail ! :(

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


Edited : 2

After I got help from @joel harkes & @dan1111 so i got regex

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

This is regex for only google.{TLD}/maps so what about maps.google.{TLD} ?

I just want to validate Google Maps URLs and urls from this way (look at the picture)

enter image description here

If possible I want to validate if address or long-lat is set (using li or q parameter check ?) (not only "maps.google.com" for example..)

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


Solution

  • Completing dan1111's answer here's 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 could not test it (jsbin is down) but it should match any url that contains at least one of ll or q parameters.

    EDIT:

    I added the maps. subdomain and fixed a small typo. After q= the + should be outside of the character class, to let it 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 the 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 the urls now.