我只是想检查该链接是否是谷歌地图链接
例如 :
var urls =[
/// correct urls
"https://www.google.com/maps",
"https://www./maps",
"https:///maps",
"http:///maps",
"https://www./maps",
"https://www.google.com/maps?ll=37.0625,-95.677068&spn=45.197878,93.076172&t=h&z=4",
"https://www./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./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./ "
];
我对Regex非常不满,并尝试使用JavaScript Regex Generator,但它对我来说仍然很难.
我只得到..
Reg = /^http\:\/\/|https\:\/\/|www\.google$/;
它失败了! 🙁
但是我为你们做了一个现场测试:http:///onuyux/1/edit?javascript,live
编辑:2
在我从@joel harkes&获得帮助之后@ dan1111所以我得到了正则表达式
/^https?\:\/\/(www\.)?google\.[a-z] \/maps\b/
这只是谷歌的正则表达式.{TLD} /地图那么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./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./ ",
"http://maps.google.com/",
"http://google.com/maps",
"http:///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:///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.
编辑:
我添加了地图.子域名并修正了一个小错字.在q =之后,应该在字符类之外,让它重复:q = [^&]而不是q = [^& amp; ]. 话虽这么说,这是你的正则表达式:
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=[^&] ) ($|&)/;
希望它现在涵盖所有网址. 来源:https://www./content-1-355151.html
|