Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
I want to fetch loginID and password from below code
const fs = require('fs');
// var fileRefer=new Array();
 var fileRefer = fs.readFileSync('D:\\NgageAuto\\LoginID\\Creds.txt').toString().split("\n");
    for(i in fileRefer) {
      console.log(fileRefer[i]);
        }

Ouput:- Date: 2021-11-08 16:56:42 LoginID: pvgA1245 Password: Root@123 it's one of the example which is in file i want LoginID value i.e "pvgA1245 and password value i.e Root@123

Please , help me how can i make it!!!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

there are hundreds of solutions to this problem, some having advantages in different scenarios then others.

Here are some for using split() or using a regex match. Each either using destructuring or just "normally assign" the variables. Regex has the advantage, that it can be used more flexible for example if sometimes the line schema is different and Password is missing. But if you can be sure that the schema is always the same or just wanna skip lines that don't have all values, split() is totally fine.

You would just add the relevant code snippet inside your for loop

let i = "Date: 2021-11-08 16:56:42 LoginID: pvgA1245 Password: Root@123";

// split and destructure
const [ , , , , id, ,pw,] = i.split(" ");
console.log(id, pw);


// split and normally assign
const a = i.split(" ");
const id2 = a[4];
const pw2 = a[6];
console.log(id2, pw2);

// regex and destructure
const [, id3, pw3] = i.match(/(?:LoginID: ([^\s]*)) ?(?:Password: ([^\s]*))/);
console.log(id3, pw3);

// regex and normally assign
const m = i.match(/(?:LoginID: ([^\s]*)) ?(?:Password: ([^\s]*))/);
const id4 = m[1];
const pw4 = m[2];
console.log(id4, pw4);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!