Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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
JS regex仅排除http-必须包括https_Regex_Regex Negation - Fatal编程技术网

JS regex仅排除http-必须包括https

JS regex仅排除http-必须包括https,regex,regex-negation,Regex,Regex Negation,我需要使用Javascript来验证URL输入,其中除了不安全的http协议之外的所有URL都是有效的 有效URL的示例: https://example.com/ https://example.com/path application://example.com/path application://example 无效URL的示例: http:\\example.com 我尝试过消极前瞻,但不起作用: ^(?!http[s]{0}).*:\/\/.*$ 如何编写只排除http协议的正

我需要使用Javascript来验证URL输入,其中除了不安全的http协议之外的所有URL都是有效的

有效URL的示例:

https://example.com/
https://example.com/path
application://example.com/path
application://example
无效URL的示例:

http:\\example.com
我尝试过消极前瞻,但不起作用:

^(?!http[s]{0}).*:\/\/.*$

如何编写只排除http协议的正则表达式(必须包括其他协议,如https)?

您可以这样使用它:

^(?!http:)[^:]+:\/\/.+$
如果字符串以
http:
开头,则
(?!http:)
为负前瞻,将导致匹配失败,从而导致
http://...
URL


但是请注意,此正则表达式不进行复杂的URL验证,它只负责处理
http:
URL/

的匹配失败。您可以使用
URL api
并将
协议
属性与
http:

让urlTester=(url)=>{
让urlParsed=新URL(URL)
返回urlParsed.protocol!=='http:'
}
console.log(urlTester('https://example.com/'))
console.log(urlTester('https://example.com/path'))
console.log(urlTester('application://example.com/path'))
console.log(urlTester('application://example'))

console.log(urlTester('http://example.com)
如果
application://example.com/path
安全吗?