4

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

l2aelba
  • 21,591
  • 22
  • 102
  • 138
  • 1
    +1 for the live test. Very helpful. –  Feb 06 '13 at 13:17
  • 1
    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. ^^ – Jon Feb 13 '13 at 08:31
  • but just one error now : http://jsbin.com/onuyux/25/edit?javascript,live @Jon – l2aelba Feb 13 '13 at 08:48
  • Oh just maps.google.com get error – l2aelba Feb 13 '13 at 08:53
  • 1
    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. – Jon Feb 13 '13 at 09:01
  • @jon http://jsbin.com/onuyux/31/edit?javascript,live Can you look at this ? But I want to check if domainname if `google` also – l2aelba Feb 13 '13 at 09:20
  • 1
    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} – Jon Feb 13 '13 at 09:43
  • 2
    Don't forget about domains like `google.co.uk`. – Qtax Feb 15 '13 at 00:12

4 Answers4

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

Joel Harkes
  • 10,975
  • 3
  • 46
  • 65
  • Thanks so much :D , Your regex is so simple :D Work ! http://jsbin.com/onuyux/5/edit?javascript,live – l2aelba Feb 06 '13 at 12:47
  • you can use `(https|http)://(www\.|)google\.[a-z]+/maps($|\?)` which will check if its end of string or followed by a ? – Joel Harkes Feb 06 '13 at 13:07
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.

Community
  • 1
  • 1
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.

fejese
  • 4,601
  • 4
  • 29
  • 36
  • 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 ? – l2aelba Feb 12 '13 at 14:42
  • + added link with `q` parameter in my list now :D http://jsbin.com/onuyux/17/edit?javascript,live – l2aelba Feb 12 '13 at 14:45
  • @l2aelba So to clarify, you want to check that both ll and q is set, or either one of those? – fejese Feb 12 '13 at 14:46
  • 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 ? – l2aelba Feb 12 '13 at 14:48
  • i have to go now , coming to look your regex asap. Thanks anyway :D – l2aelba Feb 12 '13 at 14:50
  • 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 `ll` – l2aelba Feb 13 '13 at 08:20
  • 1
    @l2aelba 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 :) – fejese Feb 13 '13 at 17:05
  • Can you input this link in your answer ? http://jsbin.com/onuyux/37/?javascript,live – l2aelba Feb 14 '13 at 10:10
  • Yeah ! After @Qtax comment domain like google.co.uk get error http://jsbin.com/onuyux/38/edit – l2aelba Feb 15 '13 at 08:17
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

OmegaOuter
  • 375
  • 1
  • 11