2017-03-02 11 views
-2

私は以下のコードで単線データをresultsにプッシュするので、以下のケースではIDに基づいてデータを分割することができます。ID目標は、複数の行でも、そのIDの完全なデータを取得することです。IDに基づいてデータを分割する方法は?

[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

file.txtを

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

すべてのあなたの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

これは私がここで達成しようとしているものではない、私はそれらのIDSに基づいて検索を行うことができるようにIDを持つ必要があります – hussain

関連する問題