2

私はこの素晴らしいプラグインを使用しています: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の投稿を見たところ、うまく機能しています...問題は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
4

1 に答える 1

1

私が見つけたものから、ここで見つかった正規表現(元々はDaringFireballのJohnGruberによって作成され、 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`
        },
于 2011-05-09T21:36:12.000 に答える