How to replace #text with a link? [duplicate]

安稳与你 提交于 2021-02-11 15:26:39

问题


I have created a post in Django. When a user types text and hits submit it shows in the page. Now I want to turn any hashtags in the text in to links. For example if a user types #sometext it will show a link in the HTML replacing #sometext. I was working on this and i got an error:

Uncaught SyntaxError: Unexpected token ','

$(document).ready(function() {
  $('.reply-btn').click(function() {
    $(this).parent().parent().next('.replied-comments').fadeToggle()
  });
});

$(document).ready(function() {
  function updateHashLinks() {
    $("p").each(function(data) {
      var hashtagRegex = /(^|\s)#([\w\d-]+)/g
      var newText = $(this).html().replace(hashtagRegex, "$1<a href='/tags/$2/'>#$2</a>")
      $(this).html(newText)
    });
  },
});

来源:https://stackoverflow.com/questions/60071985/how-to-replace-text-with-a-link

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!