【问题标题】:jQuery Linkify plugin: Can't handle links that end in parenthesisjQuery Linkify 插件:无法处理以括号结尾的链接
【发布时间】:2011-05-02 15:10:05
【问题描述】:

我正在使用这个很棒的插件:https://github.com/maranomynet/linkify/blob/master/1.0/jquery.linkify-1.0.js

链接操纵dom的文本。问题在于这样的链接:http://en.wikipedia.org/wiki/The_Godfather_(novel)

链接将是“http://en.wikipedia.org/wiki/The_Godfather_(novel”)

我可以在 linkify 代码中更改哪些内容以处理括号等?

谢谢!

PS:嘿,看来 Stackoverflow 也可以使用它!哈哈 ;)

编辑:

我刚刚在 DaringFireball 上看到了帖子,它运行良好...问题在于简单的 URL,例如 www.google.com(我认为它与“noProtocolUrl”的第一个正则表达式有关。这就是我马上就到了:

var noProtocolUrl = /(^|["'(\s]|<)(www\..+?\..+?)((?:[:?]|\.+)?(?:\s|$)|>|[)"',])/g,
    httpOrMailtoUrl = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:(?:[^\s()<>.]+[.]?)+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi,
        linkifier = function (html) {
            return FormatLink(html
                        .replace(noProtocolUrl, '$1<a href="<``>://$2" rel="nofollow external" class="external_link">$2</a>$3')  // NOTE: we escape `"http` as `"<``>` to make sure `httpOrMailtoUrl` below doesn't find it as a false-positive
                        .replace(httpOrMailtoUrl, '<a href="$1" rel="nofollow external" class="external_link">$1</a>')
                        .replace(/"<``>/g, '"http'));  // reinsert `"http`
        },

使用“www.facebook.com”我得到了这个(rel 和 class 属性就像链接旁边的文本:

www.facebook.com" rel="nofollow external" class="external_link">www.facebook.com

【问题讨论】:

  • 链接 in 括号呢? (example.com)
  • 科比,我真的没有想到……我觉得当时做不到……嗯……
  • Kobi 的评论大概是它以这种方式工作的原因。但是,在我看来,应该可以实现如下逻辑:如果 URL 中有一个 (,并且在那之后没有 ),那么假设最后的 ) 是一部分的网址。否则,假设它不是。或者,如果您想获得真正的花哨,您可以在没有括号的情况下对 URL 进行 AJAX HEAD 调用。如果您得到 404,请将其与括号链接起来。不这样做可能有一些很好的理由,但这可能是一个有趣的实验。

标签: jquery regex dom linkify


【解决方案1】:

根据我的发现,the regex expression found here(最初由 John Gruber of Daring Fireball 创建,由 naren1012 修改)似乎可以解决问题。

要实现,请替换此代码:

 var noProtocolUrl = /(^|["'(\s]|&lt;)(www\..+?\..+?)((?:[:?]|\.+)?(?:\s|$)|&gt;|[)"',])/g,
      httpOrMailtoUrl = /(^|["'(\s]|&lt;)((?:(?:https?|ftp):\/\/|mailto:).+?)((?:[:?]|\.+)?(?:\s|$)|&gt;|[)"',])/g,
      linkifier = function ( html ) {
          return html
                      .replace( noProtocolUrl, '$1<a href="<``>://$2">$2</a>$3' )  // NOTE: we escape `"http` as `"<``>` to make sure `httpOrMailtoUrl` below doesn't find it as a false-positive
                      .replace( httpOrMailtoUrl, '$1<a href="$2">$2</a>$3' )
                      .replace( /"<``>/g, '"http' );  // reinsert `"http`
        },

使用此代码:

 var noProtocolUrl = /(^|["'(\s]|&lt;)(www\..+?\..+?)((?:[:?]|\.+)?(?:\s|$)|&gt;|[)"',])/g,
  httpOrMailtoUrl = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:(?:[^\s()<>.]+[.]?)+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»����]))/gi,
      linkifier = function ( html ) {
          return html
                      .replace( noProtocolUrl, '$1<a href="<``>://$2">$2</a>$3' )  // NOTE: we escape `"http` as `"<``>` to make sure `httpOrMailtoUrl` below doesn't find it as a false-positive
                      .replace(httpOrMailtoUrl, '<a href="$1">$1</a>')
                      .replace( /"<``>/g, '"http' );  // reinsert `"http`
        },

【讨论】:

  • 布拉德,这不起作用,但我认为这可能是因为正则表达式上的最后一个字符显示错误:«»â��â��â��â��
  • 谢谢布拉德,我刚刚在 DaringFireball 中看到了原始帖子,它正在工作!我现在对“www.simple.com”链接有一点问题,我编辑了问题......
猜你喜欢
  • 2017-03-13
  • 2016-09-17
  • 2017-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多