有条件地使用锚标记替换https Javascript

时间:2012-08-28 10:06:15

标签: javascript jquery regex

我正在使用以下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>" /> 我怎么能避免这个......

1 个答案:

答案 0 :(得分:0)

你标记了jQuery,所以:

$('a').addClass('disableUrl').attr('rel', 'nofollow');
相关问题