简体   繁体   English

在JavaScript中处理和过滤具有相似模式的大字符串

[英]Manipulate and filter a large string with similar pattern in JavaScript

I have a thousands line of chrome console log with the similar pattern as below. 我有数千行的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

What I want to do is that, between two 10:52:52, if the string contains a key word: color (eg. ColorPicker.js), remove the part of the string from the first of "10:52:52" to the end "10:52:52". 我想做的是在两个10:52:52之间,如果字符串包含一个关键字:color(例如ColorPicker.js),请从“ 10:52:52”的第一个中删除字符串的一部分至“ 10:52:52”。 for example, because bellowing string contains key word color, the whole string should be removed. 例如,由于波纹管字符串包含关键字颜色,因此应删除整个字符串。

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

then the manipulated remaining string is: 那么剩余的操作字符串为:

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

To be more specifically, this is my pseudo-code: 更具体地说,这是我的伪代码:

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

First of all, you will need to break the log into blocks, you can split your string by line break, and then iterate over the given array. 首先,您需要将日志分成多个块,可以通过换行符分割字符串,然后遍历给定的数组。 After you got an array of lines, you will need to split each line and decide if it belongs to some block or not. 得到一行线后,您将需要分割每行并确定它是否属于某个块。

Whenever you will have an array of block you can search your keyword and if found, remove the block. 每当您有一个块数组时,您都可以搜索关键字,如果找到了关键字,则将其删除。

 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')); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM