2017-03-02 121 views
-2

我正在讀取文件中的數據,所以下面的代碼將奇異線數據推送到results如何在下面的情況下根據ID拆分數據ID。目標是獲取該ID甚至是多行的完整數據。如何根據ID分割數據?

Ctrl.js

var results = []; 
    var lines = data.split('\n'); // get the lines 
      lines.forEach(function(line) { // for each line in lines 
       if (line.indexOf(searchStr) != -1) { // if the line contain the searchSt 
        results.push({ 
        filename:logfile.filename, 
        value:line 
        }); 
       } 
      }); 

file.txt的

[ID:81d166ed-dcd6-4f30-bcf4-7c3c1a8feefb]testid Lorem test Ipsum is simply dummy text text of the printing and typesetting ind 
[ID:8a3b2de6-742e-49e4-bf96-02d14e6b0799]testid Lorem test Ipsum is simply dummy text text of the printing and typesetting industry. 
[ID:c3d19dfe-d803-4661-a0c4-52a18aa48766]testid Lorem test Ipsum is simply dummy 
+0

是什麼'searchStr'值? – RomanPerekhrest

+0

基本上它的搜索功能,所以'searchStr'是客戶端的用戶輸入,我們必須匹配來自文件的數據併發送該事件的完整數據 – hussain

+0

不清楚。 *我如何分割數據基於ID *。應該如何看待預期的結果? – RomanPerekhrest

回答

0

如果所有的ID是相同的長度,它看起來像他們,你lines.forEach裏面,你可以把這個提取ID。不完全確定你想要怎麼處理該行的其他部分,但是看起來它會抓取該ID。

var id = line.substr(4,36) 

解釋,只是從分割中抓住該位置的字符串中的文本,似乎應該始終在同一個地方。有更強大的方法,但在這種情況下,它似乎是一個容易理解的例子。

0

只是你的foreach循環之前添加:

for(var i = 0; lines.length > i ; i = i + 1){ 
    lines[i] = lines[i].replace(/\[ID:([a-z0-9]+-)+([a-z0-9])+\]/g, ''); 
} 

應該的結果,而你的[ID:值]數組,爲您例如,它會返回:

"testid Lorem test Ipsum is simply dummy text text of the printing and typesetting ind" 
"testid Lorem test Ipsum is simply dummy text text of the printing and typesetting industry." 
"testid Lorem test Ipsum is simply dummy" 
+0

多數民衆贊成那不是我想在這裏實現,我需要有ID,所以我可以根據這些IDS進行搜索 – hussain