我有数千行的chrome控制台日志,其格式如下。
10:52:52.041 VM757:15 __popover4
10:52:52.041 VM757:16 Error: dummy
at fnClass._createPopover (ColorPicker.js:339)
at fnClass.init (ColorPicker.js:127)
at fnClass.constructor (ManagedObject-dbg.js?eval:451)
at fnClass.constructor (Element-dbg.js?eval:99)
at fnClass.constructor (Control-dbg.js?eval:103)
10:52:52.041 VM757:15 __layout331
10:52:52.042 VM757:16 Error: dummy
at fnClass.constructor (ManagedObject-dbg.js?eval:385)
at fnClass.constructor (Element-dbg.js?eval:99)
at fnClass.constructor (Control-dbg.js?eval:103)
at new fnClass (Metadata-dbg.js?eval:346)
10:52:52.042 VM757:15 __chooser4
10:52:52.042 VM757:16 Error: dummy
我想要做的是,在两个10:52:52之间,如果字符串包含一个关键字:color(例如ColorPicker.js),则删除第一个字符串中的部分字符串。 10:52:52"到最后" 10:52:52"。例如,因为下划线字符串包含关键字颜色,应删除整个字符串。
10:52:52.041 VM757:16 Error: dummy
at fnClass._createPopover (ColorPicker.js:339)
at fnClass.init (ColorPicker.js:127)
at fnClass.constructor (ManagedObject-dbg.js?eval:451)
at fnClass.constructor (Element-dbg.js?eval:99)
at fnClass.constructor (Control-dbg.js?eval:103)
10:52:52.041 VM757:15 __layout331
然后被操纵的剩余字符串是:
10:52:52.041 VM757:15 __popover4
10:52:52.042 VM757:16 Error: dummy
at fnClass.constructor (ManagedObject-dbg.js?eval:385)
at fnClass.constructor (Element-dbg.js?eval:99)
at fnClass.constructor (Control-dbg.js?eval:103)
at new fnClass (Metadata-dbg.js?eval:346)
10:52:52.042 VM757:15 __chooser4
10:52:52.042 VM757:16 Error: dummy
更具体地说,这是我的伪代码:
var sOriginal = import log file;
while(sOriginal is not reach the end){
var bShouldBeRemoved = false;
var bStartedPointFound = false;
if(bStartedPointFound == false && string match "10:52:52"){
mark it as the start point;
bStartedPointFound = true;
while(!string reach the next "10:52:52" || !reach end of the string){
if(string contains "color"){
bShouldBeRemoved = true;
}
}
if(bShouldBeRemoved === true){
remove the part of the string from the first "10:52:52"
}
}
}
return sResult
答案 0 :(得分:0)
首先,您需要将日志分成块,您可以按换行符拆分字符串,然后遍历给定的数组。 获得一系列行后,您需要拆分每一行并确定它是否属于某个块。
每当你有一个块数组时,你可以搜索你的关键字,如果找到,删除该块。
const log = `10:52:52.041 VM757:15 __popover4
10:52:52.041 VM757:16 Error: dummy
at fnClass._createPopover (ColorPicker.js:339)
at fnClass.init (ColorPicker.js:127)
at fnClass.constructor (ManagedObject-dbg.js?eval:451)
at fnClass.constructor (Element-dbg.js?eval:99)
at fnClass.constructor (Control-dbg.js?eval:103)
10:52:52.041 VM757:15 __layout331
10:52:52.042 VM757:16 Error: dummy
at fnClass.constructor (ManagedObject-dbg.js?eval:385)
at fnClass.constructor (Element-dbg.js?eval:99)
at fnClass.constructor (Control-dbg.js?eval:103)
at new fnClass (Metadata-dbg.js?eval:346)
10:52:52.042 VM757:15 __chooser4
10:52:52.042 VM757:16 Error: dummy`;
function cleanLog(log, term) {
const lines = log.split("\n");
const blocks = lines.reduce((acc, line) => {
if (/^\d+:\d+/.test(line)) {
acc.push([line]);
} else {
acc[acc.length - 1].push(line);
}
return acc;
}, []);
return blocks.filter((block) => block.join('').indexOf(term) === -1)
}
console.log(cleanLog(log, 'Color'));