Javascript 使用nodejs在html页面中捕获错误消息
我使用Nodejs检查html文件中是否有php生成的错误/警告/通知 我要做的第一件事是在body标记上使用这个正则表达式捕获这些错误:Javascript 使用nodejs在html页面中捕获错误消息,javascript,html,node.js,regex,Javascript,Html,Node.js,Regex,我使用Nodejs检查html文件中是否有php生成的错误/警告/通知 我要做的第一件事是在body标记上使用这个正则表达式捕获这些错误: const warningAndError=/(?:(?:.*\n){0,0})。*?(警告|错误|通知)。*(?:(?:\n.*)/gm 这将返回如下列表: ["Error: Here goes the err message on line 571", "Warning: Here goes the warn mess
const warningAndError=/(?:(?:.*\n){0,0})。*?(警告|错误|通知)。*(?:(?:\n.*)/gm代码>
这将返回如下列表:
["Error: Here goes the err message on line 571",
"Warning: Here goes the warn message on line 700"]
[chalk.red.bold("Error: ") + "Here goes the err message on line 571",
chalk.yellow.bold("Warning: ") + "Here goes the warn message on line 700"]
我接下来要做的是创建如下数组:
["Error: Here goes the err message on line 571",
"Warning: Here goes the warn message on line 700"]
[chalk.red.bold("Error: ") + "Here goes the err message on line 571",
chalk.yellow.bold("Warning: ") + "Here goes the warn message on line 700"]
因此,我的代码如下所示:
const warningAndError = /(?:(?:.*\n){0,0}).*?<b>(Warning|Error|Notice)<\/b>.*(?:(?:\n.*){0,0})/gm;
const errList = [];
(body.match(warningAndError) || []).forEach((err) => {
let strippedString = err.replace(/(<([^>]+)>)/gi, "");
if (strippedString.startsWith("Error:")) {
errList.push(
chalk.red.bold("Error: ") + strippedString.replace("Error:", "").trim()
);
}
if (strippedString.startsWith("Warning:")) {
errList.push(
chalk.yellow.bold("Warning: ") + strippedString.replace("Warning:", "").trim()
);
}
if (strippedString.startsWith("Notice:")) {
errList.push(
chalk.green.bold("Notice: ") + strippedString.replace("Notice:", "").trim()
);
}
});
const warningAndError=/(?:(?:.*\n){0,0})。*?(警告|错误|通知)。*(?:(?:\n.*{0,0})/gm;
常量errList=[];
(body.match(warningAndError)| |[]).forEach((err)=>{
让strippedString=err.replace(/(]+)>)/gi,“”;
if(strippedString.startsWith(“错误:)){
errList.push(
chalk.red.bold(“错误:)+strippedString.replace(“错误:,”).trim()
);
}
if(strippedString.startsWith(“警告:)){
errList.push(
粉笔。黄色。粗体(“警告:)+条纹字符串。替换(“警告:,”)。修剪()
);
}
if(strippedString.startsWith(“注意:)){
errList.push(
粉笔。绿色。粗体(“注意:”)+条纹字符串。替换(“注意:”,“”)。修剪()
);
}
});
我遇到的问题是,这不是干净的,所以我想知道是否有更干净的方法来做,另一件事是,它需要很长时间才能执行超过2秒
如果这有帮助,以下是html页面中错误的外观:
错误:第571行出现错误消息
警告:第700行出现警告消息
提前感谢。使用应该可以做到:
const levels={
错误:“红色”,
警告:“黄色”,
注意:“绿色”
}
const regex=new RegExp(`(${Object.keys(levels.join('|'))}):([^`${{u}}},
绿色:{bold:\=>`${}`}
}
document.body.innerHTML=粉笔化(document.body.innerHTML).join(“
”)
错误:第571行出现错误消息
警告:第700行出现警告消息
应该做什么?@xehpuk如果我不做第一个{0,0}
它将返回
以便在错误行x{0,0}
表示“重复x零次”之前不捕获该行,因此它与空字符串相同。您当前的正则表达式可以简化为/.*(警告|错误|注意)。*/gm
。