gpt4 book ai didi

javascript - 用于 URL 验证的正则表达式 - Javascript ( AngularJs )

转载 作者:行者123 更新时间:2023-12-03 02:30:08 26 4
gpt4 key购买 nike

我需要以下 URL 应返回 true

以下网址应返回 false

这是我到目前为止尝试过的正则表达式 /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+ )+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/.我使用这个内部 Angular 形式输入 ng-patter

如有任何帮助,我们将不胜感激

最佳答案

尝试这个正则表达式

/^((?:http://)|(?:https://))(www.)?((?:[a-zA-Z0-9]+.[a-z]{3} )|(?:\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}(?::\d+)?))([/a -zA-Z0-9.]*)$/gm

function isUrlValid(userInput) {
var res = userInput.match(/^((?:http:\/\/)|(?:https:\/\/))(www.)?((?:[a-zA-Z0-9]+\.[a-z]{3})|(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?::\d+)?))([\/a-zA-Z0-9\.]*)$/gm);
if(res == null)
return false;
else
return true;
}

关于javascript - 用于 URL 验证的正则表达式 - Javascript ( AngularJs ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48764209/

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