gpt4 book ai didi

javascript regexp 用于验证开头不带 http 的 url

转载 作者:行者123 更新时间:2023-11-28 09:43:10 25 4
gpt4 key购买 nike

我想使用 URL 的正则表达式来验证 JavaScript 字符串。

我不想一开始就强制使用 http://。

正确的正则表达式是什么,因为我有下一个要包含 http:

var pattern = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;

最佳答案

如果您想使用已有的正则表达式,但将 ftp、http 和 https 设为可选。请参阅以下内容:

var pattern = /((ftp|http|https):\/\/)?(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;

关于javascript regexp 用于验证开头不带 http 的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12129914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com