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.
Asked
Active
Viewed 251 times
2
Rick Hitchcock
- 34,132
- 4
- 45
- 75
mikesrike
- 111
- 3
-
2this 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 Answers
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,168
- 3
- 20
- 33
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?