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

  • 源码大小:6.61KB
  • 所需积分:1积分
  • 源码编号:19JP-3391
  • 浏览次数:637次
  • 最后更新: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。

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

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

  1. <!-- Output -->
  2. Hi my twitter account is <a href="http://twitter.com/#!/jqueryscript">@jqueryscript</a>.
  3.  
  4. 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>
  5.  
  6. Contact me at <a href="mailto:[email protected]">[email protected]</a>
  7.  
  8. This below script is not going to execute because of encoding
  9.  
  10. &lt;script&gt;alert('hello');&lt;/script&gt;

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

  1. lt : {
  2. regex : /(<)/g,
  3. template : '&lt;'
  4. },
  5. gt : {
  6. regex : /(>)/g,
  7. template : '&gt;'
  8. },
  9. dq : {
  10. regex : /(")/g,
  11. template : '&quot;'
  12. },
  13. sq : {
  14. regex : /(')/g,
  15. template : '&#x27;'
  16. },
  17. link : {
  18. regex : /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,
  19. template : "<a href=\"$1\">$1</a>"
  20. },
  21. user : {
  22. regex : /((?:^|[^a-zA-Z0-9_!#$%&*@ï¼ ]|RT:?))([@ï¼ ])([a-zA-Z0-9_]{1,20})(\/[a-zA-Z][a-zA-Z0-9_-]{0,24})?/g,
  23. template : '$1<a href="http://twitter.com/#!/$3$4">@$3$4</a>'
  24. },
  25. hash : {
  26. regex : /(^|\s)#(\w+)/g,
  27. template : '$1<a href="http://twitter.com/#!/search?q=%23$2">#$2</a>'
  28. },
  29. email : {
  30. regex : /([a-zA-Z0-9._-][email protected][a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi,
  31. template : '<a href=\"mailto:$1\">$1</a>'
  32. }

预览截图