Google Maps link validation using Regex
Asked Answered
S

6

5

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

Stowers answered 6/2, 2013 at 12:32 Comment(8)
+1 for the live test. Very helpful.Eisen
Per your comment "This is regex for only google.{TLD}/maps so what about maps.google.{TLD}", javascript doesn't support look-behinds, so you would need to create another regular expression specifically for "maps.google.com", etc that doesn't have to match '/maps' at the end. After you create the second expression, check with both of them, if one matches, then it is good, if none match then it isn't. ^^Amaranthine
but just one error now : jsbin.com/onuyux/25/edit?javascript,live @AmaranthineStowers
Oh just maps.google.com get errorStowers
According to that last live example, it's because the regex doesn't allow for (www\.|maps\.) which would fix it for that one line, but fail if the url is "maps.google.com/?q=" (which is valid if everything after the question mark matches your other conditions.). If you have 'maps.google', you don't need '/maps' at the end, which is where back-referencing would come in to play in a language other than JS. If you have '(www\.|maps\.)' and then make '/maps' conditional as well, it would be too flexible - thus why you need to compare against two regex in order to match all possible urls.Amaranthine
@jon jsbin.com/onuyux/31/edit?javascript,live Can you look at this ? But I want to check if domainname if google alsoStowers
I would have kept the original regex you had, but then add a new one that had it as (maps\.)? instead of 'www.', and on that one make \/(maps)? after the {TLD}Amaranthine
Don't forget about domains like google.co.uk.Andalusia
R
2

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.

Robinet answered 12/2, 2013 at 14:36 Comment(9)
error : 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 (for example) q parameter ?Stowers
+ added link with q parameter in my list now :D jsbin.com/onuyux/17/edit?javascript,liveStowers
@Stowers So to clarify, you want to check that both ll and q is set, or either one of those?Robinet
all working links :D some user add by name or same user pointing to position like long-lat :D Your regex is work but can you add q parameter also ?Stowers
i have to go now , coming to look your regex asap. Thanks anyway :DStowers
done with this : /^https?\:\/\/(www\.)?google\.[a-z]+\/maps\/?\?([^&]+&)*|(ll=-?[0-9]{1,2}\.[0-9]+,-?[0-9]{1,2}\.[0-9]+|q=[^&+])+($|&)/it's right regex ? I just add | before llStowers
@Stowers No it's not. The regex you made will match to both https?\:\/\/(www\.)?google\.[a-z]+\/maps\/?\?([^&]+&)*|(ll=-?[0-9]{1,2}\.[0-9‌​]+,-?[0-9]{1,2}\.[0-9]+ OR (ll=-?[0-9]{1,2}\.[0-9‌​]+,-?[0-9]{1,2}\.[0-9]+|q=[^&+])+($|&). I updated my answer to make it better :)Robinet
Can you input this link in your answer ? jsbin.com/onuyux/37/?javascript,liveStowers
Yeah ! After @Andalusia comment domain like google.co.uk get error jsbin.com/onuyux/38/editStowers
B
2
(https|http)://(www\.|)google\.[a-z]+/maps

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

that regex selects only the good urls not the bad, but doesnt check enny \GET variables

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

you can add ^ in front to test if it is start of string

Ballistic answered 6/2, 2013 at 12:38 Comment(2)
Thanks so much :D , Your regex is so simple :D Work ! jsbin.com/onuyux/5/edit?javascript,liveStowers
you can use (https|http)://(www\.|)google\.[a-z]+/maps($|\?) which will check if its end of string or followed by a ?Ballistic
E
2

Here is a simple regex that will work on 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 separate problems here:

  • Verify that something is a valid URL (I assume you need to do this because one of your examples is an invalid URL).
  • Verify that a URL is a Google Maps URL.

If you want a robust solution, I suggest doing it in a two-step process. First determine that the URL is valid, then see if it is a Google Maps URL.

The first part is quite complex in itself, but many others have tackled it before. If you search for "Javascript URL Validation" you will see many questions and many proposed answers. I suggest this one as a place to start, but I don't have any personal experience of doing this in Javascript.

For the second part, you have to determine exactly what a Google Maps URL can look like. You have given some examples above, but they are clearly a subset of the possibilities. How exhaustive do you want to be? And do you need to verify that the parameters are valid, as well? This could be a huge task.

Eisen answered 6/2, 2013 at 12:57 Comment(2)
sorry , my fail. Not only (com|fr|de) and only maps not mapsz (for example)Stowers
I got now : /^https?\:\/\/(www\.)?google\.[a-z]+\/maps\b/ thanksStowers
R
2

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.

Robinet answered 12/2, 2013 at 14:36 Comment(9)
error : 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 (for example) q parameter ?Stowers
+ added link with q parameter in my list now :D jsbin.com/onuyux/17/edit?javascript,liveStowers
@Stowers So to clarify, you want to check that both ll and q is set, or either one of those?Robinet
all working links :D some user add by name or same user pointing to position like long-lat :D Your regex is work but can you add q parameter also ?Stowers
i have to go now , coming to look your regex asap. Thanks anyway :DStowers
done with this : /^https?\:\/\/(www\.)?google\.[a-z]+\/maps\/?\?([^&]+&)*|(ll=-?[0-9]{1,2}\.[0-9]+,-?[0-9]{1,2}\.[0-9]+|q=[^&+])+($|&)/it's right regex ? I just add | before llStowers
@Stowers No it's not. The regex you made will match to both https?\:\/\/(www\.)?google\.[a-z]+\/maps\/?\?([^&]+&)*|(ll=-?[0-9]{1,2}\.[0-9‌​]+,-?[0-9]{1,2}\.[0-9]+ OR (ll=-?[0-9]{1,2}\.[0-9‌​]+,-?[0-9]{1,2}\.[0-9]+|q=[^&+])+($|&). I updated my answer to make it better :)Robinet
Can you input this link in your answer ? jsbin.com/onuyux/37/?javascript,liveStowers
Yeah ! After @Andalusia comment domain like google.co.uk get error jsbin.com/onuyux/38/editStowers
U
2

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=[^&+])+($|&)/;

Can test it in http://jsbin.com/onuyux/36

Unisexual answered 14/2, 2013 at 6:49 Comment(0)
E
0

From the google's documentation I found the regex here

(http(s?)://)?
((maps\.google\.{TLD}/)|
 ((www\.)?google\.{TLD}/maps/)|
 (goo.gl/maps/))
.*

And I translated it like below:

/^(http(s?)\:\/\/)?((maps\.google\.[a-z]+\/)|((www\.)?google\.[a-z]+\/maps\/)|(goo\.gl\/maps\/)).*/

Reg exp states that the following urls are exact match:

  • maps.google.com
  • google.com/maps
  • goo.gl/maps

where .com is one of the {TLD}s.

Edit:

Adding support for iphone google maps app share button https://maps.app.goo.gl/{…}

/^(http(s?)\:\/\/)?((maps\.google\.[a-z]+\/)|((www\.)?google\.[a-z]+\/maps\/)|(goo\.gl\/maps\/)|(maps\.app\.goo\.gl\/)).*/
Electroencephalograph answered 8/5, 2024 at 15:54 Comment(0)
I
-1

you can use this regex to validate both domains "maps.apps.goo.gl" or "www.google.com"

/^(https?\:\/\/)?(maps\.app\.goo\.gl|www\.google\.com)\/.+$/

you can test this on : https://regex101.com/

Ilailaire answered 1/12, 2023 at 20:46 Comment(1)
Please considering updating your answer to include the test data provided by l2aelba. This regex needs significant changes to be applicable to the question.Rossiya

© 2022 - 2025 — McMap. All rights reserved.