将纯文本URL转换为超链接 jQuery Feedify

  • 源码大小:6.61KB
  • 所需积分:1积分
  • 源码编号:19JP-3391
  • 浏览次数:249次
  • 最后更新:2023年06月08日
  • 所属栏目:文本
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

Feedify是一款小巧快速的产品要链接的文本转换器,自动将纯文本URL转换为超链接。支持常规链接(http、https、ftp等)、Twitter@username、Twitter#标签和电子邮件地址。

此外,该插件还能够对不安全的字符(如<、>、“、'、&等)进行编码和转义,以防止XSS攻击。

参见:

  • 将文本中的URL转换为链接-linkify
  • 用于将URL转换为链接的jQuery插件-urlToLink
  • 分析文本中URL的微小jQuery插件-链接

如何使用它:

1.在jQuery之后下载并加载jQuery.feedify.js。

<script src="/path/to/cdn/jquery.slim.min.js"></script>
<script src="/path/to/jQuery.feedify.js"></script>

2.初始化目标文本上的Feediy。

<!-- Output -->
Hi my twitter account is <a href="http://twitter.com/#!/jqueryscript">@jqueryscript</a>. 

I wrote <a href="http://twitter.com/#!/search?q=%23jQuery">#jQuery</a> articles on <a href="https://www.jqueryscript.net">https://www.jqueryscript.net</a>

Contact me at <a href="mailto:[email protected]">[email protected]</a>

This below script is not going to execute because of encoding

&lt;script&gt;alert('hello');&lt;/script&gt;

3.使用正则表达式添加您自己的文本模式。

lt : {
  regex : /(<)/g,
  template : '&lt;'
},
gt : {
  regex : /(>)/g,
  template : '&gt;'
},
dq : {
  regex : /(")/g,
  template : '&quot;'
},
sq : {
  regex : /(')/g,
  template : '&#x27;'
},
link : {
  regex : /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,
  template : "<a href=\"$1\">$1</a>"
},
user : {
  regex : /((?:^|[^a-zA-Z0-9_!#$%&*@ï¼ ]|RT:?))([@ï¼ ])([a-zA-Z0-9_]{1,20})(\/[a-zA-Z][a-zA-Z0-9_-]{0,24})?/g,
  template : '$1<a href="http://twitter.com/#!/$3$4">@$3$4</a>'
},
hash : {
  regex : /(^|\s)#(\w+)/g,
  template : '$1<a href="http://twitter.com/#!/search?q=%23$2">#$2</a>'
},
email : {
  regex : /([a-zA-Z0-9._-][email protected][a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi,
  template : '<a href=\"mailto:$1\">$1</a>'
}

预览截图