简体   繁体   中英

How to extract info from TXT using node

Student no. - Class no.                                          
123123 - 123123123                                                  
Class date: abc 123                       
Year number: 123124 
Class Period:
May 12, 2020 to Aug 31, 2020
Class address: 
blah blah blah                                                                

How do I extract these info individually and store it in a variable where users can call it from a text file using node and javascript?

const fs = require('fs');
const path = require('path');

fs.readFile(path.join(__dirname, './text.txt'), { encoding: 'utf-8' }, (err, data) => {
  if (err) {
    console.log(err);
  }
  // split to lines, remove white space
  const result = data.split(/\n/g).map((line) => {
    return line.split(/\s+/).filter(Boolean);
  });
  console.log(result);

  // result content:
  // [
  //   ['Student', 'no.', '-', 'Class', 'no.'],
  //   ['123123', '-', '123123123'],
  //   ['Class', 'date:', 'abc', '123'],
  //   ['Year', 'number:', '123124'],
  //   [],
  //   ['Class', 'Period', 'period:'],
  // ];
});


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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