url.js 632 B

123456789101112131415
  1. let replaceURLWithHTMLLinks = (content, color) => {
  2. // 使用正则表达式匹配更广泛的URL格式(此正则由deepseek生成)
  3. const urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]|\bwww\.[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
  4. return content.replace(urlRegex, (url) => {
  5. // 如果URL不以http(s)://开头,则添加http://前缀
  6. if (!url.startsWith("http")) {
  7. url = "http://" + url;
  8. }
  9. return `<a href="${url}" target="_blank" style="color: ${color};text-decoration: underline;">${url}</a>`;
  10. });
  11. }
  12. export default {
  13. replaceURLWithHTMLLinks
  14. }