2017-03-02 113 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