我只想检查该链接是否为Google Maps链接
例如:
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/+"
];
我对Regex感到很糟糕,并尝试使用JavaScript Regex Generator,但对我来说仍然很难。
我只有..
Reg = /^http\:\/\/|https\:\/\/|www\.google$/;
和它的失败! :(
但是我为大家做了现场测试:http://jsbin.com/onuyux/1/edit?javascript,live
编辑:2
在我从
@joel harkes
和@dan1111
获得帮助之后,我得到了正则表达式/^https?\:\/\/(www\.)?google\.[a-z]+\/maps\b/
这是仅用于
google.{TLD}/maps
的正则表达式,那么maps.google。{TLD}呢?我只想通过这种方式验证Google Maps网址和网址(看图片)
(如果可能),我想验证是否设置了地址或经纬度(使用
li
或q
参数检查?)(例如,不仅是“maps.google.com”。)更新列表+代码:
/// 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"
在线测试: http://jsbin.com/onuyux/25/edit?javascript,live
最佳答案
完成 dan1111 的答案,这是一种与指定域匹配并检查ll GET变量是否设置的模式:
Reg = /^https?\:\/\/(www\.)?google\.[a-z]+\/maps\/?\?([^&]+&)*(ll=-?[0-9]{1,2}\.[0-9]+,-?[0-9]{1,2}\.[0-9]+|q=[^&+])+($|&)/;
我无法对其进行测试(jsbin已关闭),但它应与包含ll或q参数中至少一个的任何URL匹配。
编辑:
我添加了 map 。子域并修复了一个小的错字。在
q=
之后,+
应该在字符类之外,以使其重复:q=[^&]+
而不是q=[^&+]
。话虽如此,这是您的正则表达式:
Reg = /^https?\:\/\/(www\.|maps\.)?google\.[a-z]+\/maps\/?\?([^&]+&)*(ll=-?[0-9]{1,2}\.[0-9]+,-?[0-9]{1,2}\.[0-9]+|q=[^&]+)+($|&)/;
EDIT2 :
要接受google.co.uk样式域,请使用以下正则表达式。
Reg = /^https?\:\/\/(www\.|maps\.)?google(\.[a-z]+){1,2}\/maps\/?\?([^&]+&)*(ll=-?[0-9]{1,2}\.[0-9]+,-?[0-9]{1,2}\.[0-9]+|q=[^&]+)+($|&)/;
希望它现在能覆盖所有URL。