Javascript 用于验证URL的正则表达式
我需要一个正则表达式来验证URLJavascript 用于验证URL的正则表达式,javascript,regex,Javascript,Regex,我需要一个正则表达式来验证URL 以下是有效的URL格式: http://example.com http://example.com/index.php http://example.com/index.php#test http://example.com/demo/index.php http://example.com/demo/index.php#test http://www.example.com http://wwww.example.com https://www.exampl
以下是有效的URL格式:
http://example.com
http://example.com/index.php
http://example.com/index.php#test
http://example.com/demo/index.php
http://example.com/demo/index.php#test
http://www.example.com
http://wwww.example.com
https://www.example.com
https://wwww.example.com
://example.com
http://example.com/index..php
http://example.com/index.php###test
http://example.com/de,mo/index.php
http://example.com/de!mo/index.php#test
http://ww.example.com
http://wwwww.example.com
htps://www.example.com
https:/wwww.example.com
http://wwww.example.com/.,.,.
http://wwww.exa_mple.com/
以下是无效的URL格式:
http://example.com
http://example.com/index.php
http://example.com/index.php#test
http://example.com/demo/index.php
http://example.com/demo/index.php#test
http://www.example.com
http://wwww.example.com
https://www.example.com
https://wwww.example.com
://example.com
http://example.com/index..php
http://example.com/index.php###test
http://example.com/de,mo/index.php
http://example.com/de!mo/index.php#test
http://ww.example.com
http://wwwww.example.com
htps://www.example.com
https:/wwww.example.com
http://wwww.example.com/.,.,.
http://wwww.exa_mple.com/
试试这个
var url = "http://www.example.com";
var url_regx = /^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/;
if(url.match(url_regx))
{
alert("matched");
}
else
{
alert("unmatched");
}
var url=”http://www.example.com";
变量url\u regx=/^(ht | f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s\\\\\\\\\\\\\\\\\\\\\[\]`+)$/;
if(url.match(url\u regx))
{
警报(“匹配”);
}
其他的
{
警惕(“不匹配”);
}
参见
这是一个经常被问到的问题:我需要考虑,…,作为无效。为什么一个无效的URL正式?当然,“通常”你会期待一个“WWW”,但它是完全有效的,应该通过任何测试。这是一个家庭作业吗?它不工作,,,等等。