Search code examples
javascriptnode.jstextvardumb-data

How Can I Grab Data from Text based off of keywords


I want to take this text excerpt in node

var text = `
[INFO]

FIELD 1: 
FIELD 2: 
FIELD 3 :
`

I want to take this Plain Text and Write Everything Behind the : of each line to a var or const!!

How do I do This!


Solution

  • const lines = text.split(/\r?\n/);
    const data = {};
    
    lines.map( ( line ) => {
        if( line.indexOf(':') !== -1 ){
            let [prop, value] = line.split(':');
            data[prop.trim()] = value;
        }
    });
    

    Hope this helps you out!