JS regex仅排除http-必须包括https
我需要使用Javascript来验证URL输入,其中除了不安全的http协议之外的所有URL都是有效的 有效URL的示例: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协议的正
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
安全吗?