有这种http://123.456.789.1:4200
最终会http://example.com:4200
我需要使用JavaScript只是http://123.456.789.1
/http://example.com
如何正则表达式的网址,但排除端口?
我目前使用这个/(.*[a-zA-Z0-9])/i
得到,但它只是选择整个IP地址包括端口号。
如何才能选择只是IP地址或域名?
有这种http://123.456.789.1:4200
最终会http://example.com:4200
我需要使用JavaScript只是http://123.456.789.1
/http://example.com
如何正则表达式的网址,但排除端口?
我目前使用这个/(.*[a-zA-Z0-9])/i
得到,但它只是选择整个IP地址包括端口号。
如何才能选择只是IP地址或域名?
试试这个正则表达式。 /(http|https):\/\/.+?(?=:)/i
。
它应该匹配http
和https
uri方案。下面的正则表达式
请[仅限链接回答](http://meta.stackoverflow.com/tags/link-only-answers/info)。答案是“几乎不超过链接到外部网站”[可能会被删除](http://stackoverflow.com/help/deleted-answers) – Quentin
为了配合主机可以使用:
result = urlstring.match(/^[a-z][a-z0-9+.-]*:\/\/([a-z0-9._~%!$&'()*+,;=-][email protected])?([a-z0-9._~%-]+|\[[a-z0-9._~%!$&'()*+,;=:-]+\])/ig);
你可以这样做这种方式(没有正则表达式),假设端口我s总是在第二个冒号:
var url = "http://example.com:4200".split(":");
url[0] + url[1] // That will be "http//example.com"
虽然有用,但OP确实要求使用正则表达式。 –
IPv6怎么样?遇到这些的任何机会? – Lloyd