我尝试使用 nodeJS 开发基于网络的编译器。我的目标是为每个语法着色。比如下面的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';
}
我正在寻找为标准 i/o 和数据类型设置不同颜色的建议。
提前谢谢你。
最佳答案
您可以使用 colors为文本和背景选择颜色的包。
安装包
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
希望这对您有所帮助。
关于javascript - 如何在nodeJs中为单词着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36276473/