【问题标题】:Validate URL without any http , https or ftp验证没有任何 http 、 https 或 ftp 的 URL
【发布时间】:2016-04-18 09:15:28
【问题描述】:

我需要验证一个 url。

var urlPattern = new RegExp("(http|ftp|https)://[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?");

如果 URL 有 httphttpsftp,我可以验证。如果该 URL 没有 httpftphttps 表示我该如何验证?

【问题讨论】:

  • 如果您很高兴您当前的正则表达式是正确的,但您只想验证没有任何协议,您可以删除协议部分:(http|ftp|https)://?跨度>

标签: javascript jquery regex


【解决方案1】:

尝试以下方法:

var regex = new RegExp("^(http[s]?:\\/\\/(www\\.)?|ftp:\\/\\/(www\\.)?|www\\.){1}([0-9A-Za-z-\\.@:%_\+~#=]+)+((\\.[a-zA-Z]{2,3})+)(/(.)*)?(\\?(.)*)?"); 
var without_regex = new RegExp("^([0-9A-Za-z-\\.@:%_\+~#=]+)+((\\.[a-zA-Z]{2,3})+)(/(.)*)?(\\?(.)*)?");
var str = "url";
if(regex.test(str) || without_regex.test(str)){
    alert("Successful");
}else{
    alert("No match");
}

它接受以下网址:

https://example.com
http://example.com
ftp://example.com
www.example.com
https://www.example.com
http://www.example.com
ftp://www.example.com
example.com

【讨论】:

  • 如何改变这个 /^((http(s?)|ftp):\/\/)?[A-z0-9_\-\.]+... http 和 https。
  • TLD 的 2 或 3 个字符真的很短,请参阅 TLD list
【解决方案2】:

我希望这会有所帮助

 var url = 'http://www.Jamboo.com';
 var correctUrl= /^(ftp|http|https):\/\/[^ "]+$/.test(url);
  // true

  var r = /^(ftp|http|https):\/\/[^ "]+$/;
  r.test('http://www.Jam  boo.com');
   // false

【讨论】:

  • 如何改变这个/^((http(s?)|ftp):\/\/)?[A-z0-9_\-\.]+...检查没有http和https的URL
  • 我也需要接受不带http和https的URL
  • 尝试替换这个 /^:\/\/[^ "]+$/.test(url);
  • 抱歉我听不懂
【解决方案3】:

查看@Shubham Khatri 的正则表达式,我建议(当然,如果可能的话)使用wgetcurl 等外部工具来查询地址并检查结果。

【讨论】:

    【解决方案4】:

    正则表达式

    /(?(?:(http|https|ftp)://)?(?:((?:[^\W\s]|.|-|[:]{1})+)@ {1})?((?:www.)?(?:[^\W\s]|.|-)+[.][^\W\s]{2,4}|localhost(?=/ )|\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})(?::(\d*))?([/]? [^\s\?][/]{1})(?:/?([^\s\n\?[]{}#](?:(?= .)){1}|[^\s\n\?[]{}.#])?([.]{1}[^\s\?#])?)? (?:\?{1}([^\s\n#[]]))?([#][^\s\n]*)?)?/g

    HTML

    <input type= text id = 'url'>
    

    JS

    $('#url').on('blur', function() {
      var val = $(this).val();
      if(val.match(/\(?(?:(http|https|ftp):\/\/)?(?:((?:[^\W\s]|\.|-|[:]{1})+)@{1})?((?:www.)?(?:[^\W\s]|\.|-)+[\.][^\W\s]{2,4}|localhost(?=\/)|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?::(\d*))?([\/]?[^\s\?]*[\/]{1})*(?:\/?([^\s\n\?\[\]\{\}\#]*(?:(?=\.)){1}|[^\s\n\?\[\]\{\}\.\#]*)?([\.]{1}[^\s\?\#]*)?)?(?:\?{1}([^\s\n\#\[\]]*))?([\#][^\s\n]*)?\)?/g) != null)
          alert('success');
    })
    

    DEMO

    交替使用:

    $('#url').on('blur', function() {
        var urlPattern = new RegExp("((http|ftp|https)://)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:/~+#-]*[\w@?^=%&amp;/~+#-])?");
      var val = $(this).val();
      if(urlPattern.test(val)) {
        alert('success');
      } else {
        alert('fail')
      }
    })
    

    DEMO

    【讨论】:

    • 如何改变这个 /^((http(s?)|ftp):\/\/)?[A-z0-9_\-\.]+... http 和 https。
    • 'var urlPattern = new RegExp("((http|ftp|https)://)?[\w-]+(\.[\w-]+)+([\w .,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?");'也有效
    【解决方案5】:

    无协议的 URL 正则表达式

    /^[\w.-]+(?:\.[\w\.-]+)+[\w\-\._%~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/
    

    Try it here

    【讨论】:

    • 开头的负前瞻完全没用。
    • 你的正则表达式匹配[\w.-]+作为第一个字符,所以检查字符串不以://开头是没有用的,只需去掉前瞻,结果是一样的。
    【解决方案6】:

    匹配没有 (http|https|www) 的正则表达式

        /^(?!https?)(?!www\.?).*\..+$/g
    

    此正则表达式适用于

        google.com
        mail.google.com
        app.firebase.google.com
        google.com/something
    

    此正则表达式不适用于

        www.google.com
        http://google.com
        http://www.google.com
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      • 2020-06-22
      相关资源
      最近更新 更多