简体   繁体   中英

Replace unicode with HTML tags

I want to replace the code for IRC bold/colors in Javascript with an HTML tag depending on whether which character it is.

Example bold input:

[{{{line}}}] \u0002{{{nick}}}\u0002 was last seen \u0002{{{timeAgo}}}\u0002 whispering     sensually \u0002"{{{message}}}"\u0002

Desired output:

[{{{line}}}] <span class="bold">{{{nick}}}</span> was last seen <span class="bold">{{{timeAgo}}}</span> saying <span class="bold">"{{{message}}}"</span>

Example color input:

\u000304error!\u000304

Desired output:

<span class="color-304">error!</span>

Bonus: a way to reverse this operation. This is purely for display purposes, and when I save the template again I want to convert it back to IRC codes.

If there is a library available that meets these requirements, please point me in that direction.

I suppose you mean, like this.

t = s.replace(/\u0002(.*?)\u0002/g, "<span class=\"bold\">$1</span>");

And back again, like this.

u = t.replace(/<span class="bold">(.*?)<\/span>/g, "\u0002$1\u0002");

You didn't ask this part: The function document.write is one way to insert the created HTML into a page. The function Element.innerHTML is another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM