我有一個使用Nodejs. 此應用程式正在向 GitLab API 發出請求并從中獲取原始檔案資料。
我想讀取在另一個字串之后出現的特定字串并從中獲取所有類似的資料。我對這部分有點困惑,無法進一步進行,有人可以向我解釋如何實作這一目標嗎?
以下是示例檔案資料:我想讀取關鍵字之后出現的所有數字,Scenario:即在這種情況下我想獲取A001-D002 & K002-M002. 這些數字可以是任意隨機的,并且可以出現在檔案內容中的任何位置。我想閱讀它們并將它們存盤在該特定檔案的陣列中。
FileName: File Data
Background:
This is some random background
Scenario: A001-D002 My first scenario
Given I am sitting on a plane
When I am offered drinks
Scenario: K002-M002 My second scenario
Given when I book the uber taxi
When I get the notifications
我不明白如何遍歷檔案內容并讀取每個單詞并匹配并相應地獲取 ID。
以下是我向 GitLab 發出請求并獲取原始檔案內容的代碼
./index.js:
const express = require('express');
const http = require("http");
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 9000;
const gitlabDump = require("./controller/GitLabDump");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//Make NodeJS to Listen to a particular Port in Localhost
app.listen(port, function(){
gitlabDump.gitlabDump(type, function(data){
console.log("Completed Execution for GitLab")
process.exit();
})
}
我的./controller/GitLabDump.js:
const request = require('request');
const https = require('https');
const axios = require('axios');
exports.gitlabDump = function(callback){
var gitlabAPI = "https://gitlab.com/api/v4/projects/<project_id>/repository/files/tree/<subfolders>/<fileName>/raw?ref=master&private_token=<privateToken>";
//Make the request to the each file and read its raw contents
request(gitlabAPI, function(error, response, body) {
const featureFileData = JSON.parse(JSON.stringify(body)).toString();
console.log(featureFileData)
for(const match of featureFileData.matchAll("Scenario:")){
console.log(match);
}
callback("Completed");
})
}
我能夠列印檔案內容。有人可以解釋一下如何遍歷原始檔案內容并獲取所有必需的 id 嗎?
uj5u.com熱心網友回復:
我建議您使用一種方法,通過遍歷每一行來分析字串的每個部分(我假設您的字串與您的示例中的字串一樣)。它比使用正則運算式更容易理解和編碼。
下面的示例代表您的request回呼函式。我將代碼分成 3 個邏輯:
- 搜索檔案名
- 搜索我們感興趣的行(“場景”一詞)
- 通過過濾功能提取ID
之后,您可以輕松地更改您的 ID 過濾器 ( txt.substr(0, txt.indexOf(' ')) 以使用更合適的運算式來提取您的句子。
結果被發送到回呼函式,第一個引數是檔案名,第二個引數是所有 id。就像您在示例中所做的那樣。
((callback) => {
const featureFileData = `FileName: File Data
Background:
This is some random background
Scenario: A001-D002 My first scenario
Given I am sitting on a plane
When I am offered drinks
Scenario: K002-M002 My second scenario
Given when I book the uber taxi
When I get the notifications`;
// find "filename"
const filenames = featureFileData.split('\n')
.filter(line => line.trim().substr(0,8) === 'FileName')
.map((raw) => {
if(!raw) return 'unknown';
const sentences = raw.trim().split(':');
if(sentences[1] && sentences[1].length) {
return sentences[1].trim();
}
});
// filter the "Scenario" lines
const scenarioLines = featureFileData.split('\n')
.map((line) => {
if(line.trim().substr(0,8) === 'Scenario') {
const sentences = line.trim().split(':');
if(sentences[1] && sentences[1].length) {
return sentences[1].trim();
}
}
return false;
})
.filter(r => r !== false);
// search ids
const ids = scenarioLines.map(txt => txt.substr(0, txt.indexOf(' ')));
callback(filenames[0], ids);
})(console.log)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/433103.html
標籤:javascript 节点.js 文件 fs
上一篇:PHP中檔案中的數字總和
下一篇:在r中移動檔案時出現錯誤
