如何在nodeJs中对单词进行着色

我试图用nodeJS开发一个基于web的编译器。 我的目标是为每个语法着色。 如下面的C代码:

int var; printf("%d",var); 

我想在这里着色“int”和printf。 我已经做了以下代码,直到今天,每个字母在打字过程中都变成了红色。

 <textarea rows="13" cols="150" id="code" name="code" placeholder="do ypur code here" onfocus="if(this.value==''){ this.value='#include<stdio.h>\n' + 'int main(){\n' + 'return 0;\n' + '}'; this.style.color='green'; }" onkeyup="keyup()" onkeypress="pre()" onkeydown="down()" > function keyup() { document.getElementById('code').style.color='green'; } function pre() { document.getElementById('code').style.color='red'; } function down() { document.getElementById('code').style.color='green'; } 

我正在寻找build议,把不同的,颜色的标准I / O,数据types。

先谢谢你。

您可以使用颜色包来为文本和背景select颜色。

安装包

 npm install colors --save 

用法

 var colors = require('colors'); console.log('hello'.green); // outputs green text 

要么

 var colors = require('colors/safe'); console.log(colors.green('hello')); // outputs green text 

希望这可以帮助。