2

I'm creating socket based chat app. I would like to change my incoming messages: when an word in an message contains # it has to be surrounded by div tags. For example 'this is #an message' has to be converted to 'this is <div>#an</div> message'. I'm using jQuery.

Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79
mikesrike
  • 111
  • 3
  • 2
    this is **_a_** message. Very similar question: http://stackoverflow.com/questions/7575041/change-hash-tag-to-link-on-page-load – Turnip Apr 24 '16 at 18:11

2 Answers2

4

You can do it with RegEX like this:

var content="this is #an message";
alert(content.replace(/(#\S+)/gi,"<div>$1</div>"));
Shady Alset
  • 5,548
  • 4
  • 21
  • 34
1

This can be done via regular expressions and javascript string methods. Check out this post, it should supply everything you need. How to replace all occurrences of a string in JavaScript?

Community
  • 1
  • 1
glutengo
  • 388
  • 3
  • 13