我只想检查该链接是否是 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} 呢?
我只是想通过这种方式验证谷歌地图的网址和网址(看图)
如果可能,我想验证是否设置了地址或经纬度(使用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