javascript - 如何使用javascript检测字符串是否为URL格式?

标签 javascript url

我认为问题标题似乎可以解释一切。
我想使用 javascript 检测字符串是否为 URL 格式。
任何帮助表示赞赏。

最佳答案

尝试这个-

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

usage: if (isUrl("http://www.page.com")) alert("is correct") else alert("not correct");

关于javascript - 如何使用javascript检测字符串是否为URL格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1701898/

相关文章:

javascript - Angular 模态 : add buttons from button list

javascript - jquery 和 d3 : why does my page reload when I use $ ("svg"). 删除()?

javascript - RegExp 范围乱序且模式复杂

javascript - React 应用程序尝试绑定(bind)到 View 时出错

asp.net - 如何在 ASP.NET WebForms 中操作 url?

android - 包含拉丁字符的 URL 的 FileNotFoundException

javascript - 允许在 url 末尾为 javascript 条件 css 样式使用通配符

c# - 为什么 C# WebBrowser 中的 Url 值始终为 null?

javascript - Promise.finally 没有作为链的最后一次调用返回

javascript - 如何处理 AngularJS 在我的 URL 中格式化正斜杠以转义代码 %2F