illuzion
illuzion

Reputation: 19

Modifying Text in Javascript

I'm trying to setup a text modifier for my server, where if you type something like hello ~world~ it would italic the 'world' part. I want to do this in Javascript, but I have no idea about how I would go about doing this.

Basically here's how scripting looks for where I'm running my server off of. This is also what I basically have done now, for this text modify script.

beforeChatMessage: function(src, message, chan) {
var user = sys.name(src);
var usercolor = sys.color(src)
if (message.toLowerCase().match("~")){
var italicmessage = message.replace(message, "<i>");
sys.sendHtmlAll("<font color="+usercolor+"><timestamp/> "+user+": "+italicmessage+"", channel);
return;
}

This obviously doesn't work, so, I'm guessing the correct way to would be to use substrings? Any help would be great.

Upvotes: 0

Views: 53

Answers (2)

illuzion
illuzion

Reputation: 19

I found a way to actually do this, I tested and it seems to be working. Just using regex seems to work. Example;

bold: /[b](.*?)[/b]/gi,

Upvotes: 0

lante
lante

Reputation: 7336

You can do something like that with the contenteditable="true" attribute:

<div id="demo" contenteditable="true">Hello world!</div>

<script>
    demo.innerHTML = demo.innerHTML.replace("world", "<span style='font-style: italic'>world</span>");
</script>

Refer to this fiddle to see it working.

Upvotes: 1

Related Questions