使用正则表达式在文本中查找链接,但排除引号内的链接。

3
我正在使用jQuery和正则表达式来搜索文本字符串中的http或https,并将字符串转换为URL。如果字符串以引号开头,我需要让代码跳过该字符串。
以下是我的代码:
// Get the content
var str = jQuery(this).html();

// Set the regex string
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;

var replaced_text = str.replace(exp, function(url) {
    clean_url = url.replace(/https?:\/\//gi,'');
    return '<a href="' + url + '">' + clean_url + '</a>';
})

jQuery(this).html(replaced_text);

这是我的问题示例:
文本 计算机科学与信息学院。她在 http://www.wordpress.com 上博客,并且可以在 Twitter <a href="https://twitter.com/abcdef">@Abcdef</a> 找到。 当前的代码成功地找到以 http 或 https 开头的文本,并将其转换为 URL,但它也会转换 Twitter 的链接。如果文本以引号开头或位于 a 标签中等,则需要忽略该文本...
任何帮助都将不胜感激。
3个回答

2

如果在exp变量中添加[^"']会怎样呢?

var exp = /(\b[^"'](https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;

片段:

// Get the content
var str = jQuery("#text2replace").html();

// Set the regex string
var exp = /(\b[^"'](https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;

var replaced_text = str.replace(exp, function(url) {
    clean_url = url.replace(/https?:\/\//gi,'');
    return '<a href="' + url + '">' + clean_url + '</a>';
})

jQuery("#text2replace").html(replaced_text);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="text2replace">
The School of Computer Science and Informatics. She blogs at http://www.wordpress.com and can be found on Twitter <a href="https://twitter.com/abcdef">@Abcdef</a>.
</div>


这正是我所需要的。谢谢!@lpg - Jason
如果一个链接位于字符串的开头,它将无法与当前的正则表达式匹配。 - Wiktor Stribiżew

0
如果你真的只想忽略引号,这可能会有所帮助:
var replaced_text = $("#selector").html().replace(/([^"])(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, '$1<a href="$2">$2</a>');

0

这对我有用:

它将识别URL并将其转换为超链接,但将忽略用引号"包装的URL。

请参见下面的代码或此jsfiddle以获取工作示例。

HTML示例:

<ul class="js-replaceUrls">
    <li>
        www.link-only-www.com
    </li>
    <li>
        http://link-starts-with-HTTP.com
    </li>
    <li>
        https://www.link-starts-with-https-and-www.com
    </li>

    <a href="https://link-starts-with-https.com">
        Link in anchor tag
    </a>
</ul>

正则表达式:

/(([a-z]+:\/\/)?(([a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|local|internal))(:[0-9]{1,5})?(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&amp;]*)?)?(#[a-zA-Z0-9!$&'()*+.=-_~:@/?]*)?)(\s+|$)/gmi

jQuery:

// RECOGNIZE URLS AND CONVERT THEM TO HYPERLINKS 
// Ignore if hyperlink is found in HTML attr, like "href"
$('.js-replaceUrls').each(function(){
  // GET THE CONTENT
  var str = $(this).html();

  // SET THE REGEX STRING
  var regex = /(([a-z]+:\/\/)?(([a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|local|internal))(:[0-9]{1,5})?(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&amp;]*)?)?(#[a-zA-Z0-9!$&'()*+.=-_~:@/?]*)?)(\s+|$)/gmi;
 
  // REPLACE PLAIN TEXT LINKS BY HYPERLINKS
  var replaced_text = str.replace(regex, "<a href='$1' class='js-link'>$1</a>");

  // ECHO LINK
  $(this).html(replaced_text);
});

// DEFINE URLS WITHOUT "http" OR "https"
var linkHasNoHttp = $(".js-link:not([href*=http],[href*=https])");
        
// ADD "http://" TO "href" 
$(linkHasNoHttp).each(function() {
  var linkHref = $(this).attr("href");  
  $(this).attr("href" , "http://" + linkHref);
});

请查看这个 jsfiddle 以获取工作示例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接