Skip to content Skip to sidebar Skip to footer

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. 10:52:52.041 VM757:15 __popover4 10:52:52.041 VM757:16 Error: dummy at fnClass._createPopover

Solution 1:

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

Post a Comment for "Manipulate And Filter A Large String With Similar Pattern In JavaScript"