jQuery Linkify 插件 : Can't handle links that end in parenthesis

标签 jquery regex dom linkify

我正在使用这个很棒的插件: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_(小说”

我可以在 linkify 代码中更改什么来处理括号等?

谢谢!

PS:嘿,Stackoverflow 好像也可以用这个!哈哈;)

编辑:

我刚刚看到了 DaringFireball 上的帖子,它运行得很好...问题在于像 www.google.com 这样的简单 URL(我认为这与“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

最佳答案

据我所知,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`
        },

关于jQuery Linkify 插件 : Can't handle links that end in parenthesis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5858851/

相关文章:

javascript - 添加或更新数组元素

javascript - Javascript 的正则表达式只允许字母数字

sql - PostgreSQL - 替换 HTML 实体

javascript - 未使用的 DOM 元素会怎样?

python - 你如何使用 python 从 <abc>some Text</abc> 获取 "some Text"?

jquery - 将 JQuery UI Accordion 用于移动菜单

javascript - jquery "if"一些东西和 "else"一些东西

javascript - 如何使用 Bootstrap 在悬停时显示 Action 列表?

python - 正则表达式命名组和条件逻辑

javascript - 实际反向冒泡到 DOM 中的叶子节点