Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript URL验证正则表达式-URL仅对http有效://_Javascript_Jquery_Regex_Validation_Url - Fatal编程技术网

Javascript URL验证正则表达式-URL仅对http有效://

Javascript URL验证正则表达式-URL仅对http有效://,javascript,jquery,regex,validation,url,Javascript,Jquery,Regex,Validation,Url,它的工作原理 我有一个输入字段来输入一个网站的URL,我想检查一下,如果URL是正确的,我会给这个输入字段一个类(“确认的\确定”),然后删除一个类(“cf \需要的”),如果它是错误的,则相反 问题 如果url是用http://编写的,那么它应该是正确的,但实际上,它也是用www(www.google.ch)编写的。我要怎么换正则表达式 Javascript // CHECK WEBSITE $(".cf_required[name='website']").focusout(function

它的工作原理

我有一个输入字段来输入一个网站的URL,我想检查一下,如果URL是正确的,我会给这个输入字段一个类(“确认的\确定”),然后删除一个类(“cf \需要的”),如果它是错误的,则相反

问题

如果url是用
http://
编写的,那么它应该是正确的,但实际上,它也是用
www
www.google.ch
)编写的。我要怎么换正则表达式

Javascript

// CHECK WEBSITE
$(".cf_required[name='website']").focusout(function() {
    var myVariable = $(this).val();
    if(/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/|www\.)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/.test(myVariable)){
        $(this).addClass("validated_ok").removeClass("cf_required")
    } else {
        $(this).removeClass("validated_ok").addClass("cf_required");
    }
});

删除
| www\。

^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$

只需更改您的正则表达式,使http成为必需的

这将有助于:

(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*
(?\w+):\/\/(?[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*
希望它能帮助您缩短时间:

(\w*\W*)?\w*(\.(\w)+)+(\W\d+)?(\/\w*(\W*\w)*)*
您可以在这个奇妙的正则表达式编辑器中测试它:

查看本文:
(\w*\W*)?\w*(\.(\w)+)+(\W\d+)?(\/\w*(\W*\w)*)*
^http(s?):\/\/(www\.)?(((\w+(([\.\-]{1}([a-z]{2,})+)+)(\/[a-zA-Z0-9\_\=\?\&\.\#\-\W]*)*$)|(\w+((\.([a-z]{2,})+)+)(\:[0-9]{1,5}(\/[a-zA-Z0-9\_\=\?\&\.\#\-\W]*)*$)))|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(([0-9]|([1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]+)+)(\/[a-zA-Z0-9\_\=\?\&\.\#\-\W]*)*)((\:[0-9]{1,5}(\/[a-zA-Z0-9\_\=\?\&\.\#\-\W]*)*$)*))$
In this http is mandatory and it can take port and sub resources also.
e.g http://example.com
    https://example.qwerty.com/id
    https://example.com:8989
    http://example.qwerty.com:8989/id
    https://10.1.1.1/id
    https://10.1.1.1:9090/id