0

我正在使用下面的 javascript 代码将我的内容数据(http、https)替换为带有 class="disableUrl" rel="nofollow" 的锚标记

我的代码是

  content.replace(/\n/g,"<br>")
  #URLs starting with http://, https://, or ftp://
  replacePattern = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim
  replacedText = content.replace(replacePattern, "<a class='disableUrl' rel='nofollow'>$1</a>")

我的示例内容是

This is the about google http://www.google.com sample. This sample data has images too 
<img src="https://assets/mkmmmm"/>

在上面的内容http://www.google.com被转换为

<a href="http://www.google.com" class="disableUrl" rel="nofollow">http://www.google.com</a> 

它也在图像标签的 src 中为 https 生成锚标签。喜欢

<img src="https://assets/mmm/><img src="<a class='disableUrl' rel='nofollow'>https://assets/mmm</a>" /> 我怎么能避免这种情况 ..

4

1 回答 1

0

你标记了 jQuery,所以:

$('a').addClass('disableUrl').attr('rel', 'nofollow');
于 2012-08-28T11:51:37.240 回答