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!
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!
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!
You could parse the input with the split method. Here's a working example:
const text = `
[INFO]
FIELD 1: Foo
FIELD 2: Bar
FIELD 3 : Baz
`;
const lines = text.split('\n');
for (let line of lines){
if (line.startsWith('FIELD')) {
const [_, fieldContent] = line.split(':');
console.log(fieldContent);
}
}
/*
Output:
Foo
Bar
Baz
*/
You can modify the condition inside the if statement to suit your needs. Perhaps the colon is what identifies the fields you want to extract, for example, or some other character.