簡體   English   中英

我想從下面的代碼中獲取登錄名和密碼

[英]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]);
        }

輸出:- 日期:2021-11-08 16:56:42 LoginID:pvgA1245 密碼:Root@123 這是我想要的文件中的示例之一,我想要 LoginID 值,即“pvgA1245 和密碼值,即 Root@123

請幫幫我,我該怎么做!!!

這個問題有數百種解決方案,其中一些在不同場景中具有優勢,而另一些則具有優勢。

這里有一些使用split()或使用正則表達式匹配。 每個都使用解構或只是“正常分配”變量。 正則表達式的優勢在於,它可以更靈活地使用,例如,如果有時行模式不同且密碼丟失。 但是,如果您可以確定架構始終相同,或者只是想跳過沒有所有值的行, split()完全沒問題。

您只需在for循環中添加相關的代碼片段

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM