I apologize for obsession with hash tags, but I would like to know how to do it correctly. Add a form to add comments:

<div id="message_{id}">форма</div> <script type="text/javascript"> $(document).ready(function () { $(document).find('div[id*="message_"]').each(function () { var message = $(this).text(); message = message.replace(/@(.[^!"#&'*+,.\/;<>?\\`|~]+)/gi, '<a href="/user-$1" target="_blank">$1</a>').replace(/#([A-zА-я0-9_]+)/gi, '<a href="/?go=search&query=%23$1" target="_blank">#$1</a>'); // @login , #tag $(this).html(message); }); }); </script> 

After the page is finished loading, it searches for the entire divs with id = "message_N", N is the id.

Further, if it finds it, it first searches the @login text and replaces it with a link like <a href="/user-login" target="_blank">login</a> , and then searches for #tag and replaces them with a link like <a href="/?go=search&query=%23tag" target="_blank">#tag</a> .

So, is this implementation code hash tags correct by analogy with VK or Twitter? This script does not work, what are the errors, tell me, please?

Sincerely.

    1 answer 1

    My 5 kopecks:

     message = message.replace(/[@|#]\w+/g, function(match) { return '<a href="'+((match[0] == '@')?'/user-':'/?go=search&query=')+match.replace(/@/, '')+ '" target="_blank">'+match+'</a>'; });