[英]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. 我想用HTML标签替换Javascript中IRC粗体/颜色的代码,具体取决于它是哪个字符。
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.
这纯粹是出于显示目的,当我再次保存模板时,我想将其转换回IRC代码。
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. 您没有问过这一部分:函数
document.write
是将创建的HTML插入页面的一种方法。 The function Element.innerHTML
is another. 函数
Element.innerHTML
是另一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.