2017-09-04 79 views
0

在我下面的代碼片段試圖添加對,我知道會出來,這樣,如果它發現它,它返回一個查詢,我需要運行到屏幕上的行每行比較其他功能如何使用Javascript推送多個數組並從函數中返回它們?

error: there were errors found during runtime 12.0 
warning: incorrect info 1.0 
warning: file at 'C://localhost/myfiles' not found 0.0 
warning: file at 'C://localhost/css' not found 0.0 
warning: file at 'C://localhost/js' not found 0.0 
warning: file at 'C://localhost/assets' not found 5615612.0 

這是我試圖做的,但我不知道如何忽略數字,以便實際上可以比較線條。 所有這些都發生在第一部分運行之後,我得到的數字大於0.0。

含義這些行獲得比較:

錯誤:有運行過程中發現12.0
警告錯誤:不正確信息1.0
警告:文件在 'C://本地主機/資產' 未找到5615612.0

,這些都是我的條件:

if ("error: there were errors found during runtime 12.0" == line){ 
    //run this query for database runtimeDB 
} 
if("warning: incorrect info 1.0" == line){ 
    //this query 
    //+1 for incorrectInfoDB 
} 
if("warning: file at 'C://localhost/myfiles' not found 0.0" == line){ 
    //this query 
    //+1 for incorrectInfoDB 
} 
if("warning: file at 'C://localhost/css' not found 0.0" == line){ 
    //this query 
    //+1 for outerfilesDB 
} 
if("warning: file at 'C://localhost/css' not found 0.0" == line){ 
    //this query 
    //+1 for outerfilesDB 
} 
if("warning: file at 'C://localhost/js' not found 0.0" == line){ 
    //this query 
    //+1 for outerfilesDB 
} 
if("warning: file at 'C://localhost/assets' not found 0.0" == line){ 
    //this query 
    //+1 for outerfilesDB 
} 

什麼我以爲是使用getLines()函數,然後從該函數返回沒有最後一個數字的字符串,以便進行比較。

function trim_numbers($string) { 
    $numbers = strrchr($string, ' '); 
    return str_replace($numbers, '', $string); 
} 

的問題是,我不知道如何把陣列,讓他們都在同一個getLines()函數和我沒有寫更多相同的代碼。這意味着在打開了該功能

function getLines() { 
    var text = $('textarea').val(); 
    var lines = text.split("\n"); 
    var requiredLines = []; 
    var runtimeDB = []; 
    var incorrectInfoDB = []; 
    var outerfilesDB = []; 
    lines.forEach(function(line) { 
    number = line.match(/\d+\.\d+$/); 
    if (number != null && number[0] > 0){ 
     requiredLines.push(line); 
     if (trim_numbers(line) == "error: there were errors found during runtime"){ 
      //run this query for database runtimeDB 
      //push this query to be outputed 
     } 
     if("warning: incorrect info 1.0" == line){ 
      //this query for it database array 
      //push this query to be outputed 
     } 
    } 

    }); 
    return requiredLines; 
} 

添加的陣列爲每個數據庫組

  • runtimeDB
  • incorrectInfoDB
  • outerfilesDB

我不知道,如果我正確地解釋了邏輯 但是

  1. 第一它得到大於0.0
  2. 則它陣列推到數據庫組從值大於0.0

也較高,有一種方法,以優化我的邏輯更高的所有的值?

function getLines() { 
 
    var text = $('textarea').val(); 
 
    var lines = text.split("\n"); 
 
    var requiredLines = []; 
 
    lines.forEach(function(line) { 
 
    number = line.match(/\d+\.\d+$/); 
 
    if (number != null && number[0] > 0) 
 
     requiredLines.push(line); 
 
    }); 
 
    return requiredLines; 
 
} 
 

 

 
$(document).ready(
 
    function() { 
 
    $('#printlogs').html(getLines().join('<br>')); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<textarea style="width:100%;height:120px"> 
 
error: there were errors found during runtime 12.0 
 
warning: incorrect info 1.0 
 
warning: file at 'C://localhost/myfiles' not found 0.0 
 
warning: file at 'C://localhost/css' not found 0.0 
 
warning: file at 'C://localhost/js' not found 0.0 
 
warning: file at 'C://localhost/assets' not found 5615612.0 
 
</textarea> 
 

 
<h4>Error Log</h4> 
 
<printlogs id="printlogs" style="color:red"></printlogs> 
 
<br> 
 
<p>after these logs I want to get the arrays of each group that has their own database queries for the ones printed in printlogs</p> 
 

 
<p> 
 
    ex. 
 
</p> 
 
<h4>runtimeDB</h4> 
 
<p> 
 
    //run this query for this line 
 
</p> 
 
<h4>incorrectInfoDB</h4> 
 
<p> 
 
    //run this query for this line<br> //run this other query for other line ... 
 
</p>

+0

我是否明白所有你想要的是解析最後沒有編號的行,所以直到'找到',以便你可以將它們與你的條件進行比較? – deg

+0

是的,我想比較那些值大於0.0的值與我自己的行(沒有數字)的預定列表以及該行所屬的查詢。 – learningbyexample

+0

你寫這個日誌嗎?如你所說,你是否能夠輸出與目前不同的內容? – deg

回答

0

我會使用一個正則表達式匹配,並可能爲以後比較中使用特定的關鍵字。

在這個特定的正則表達式中,我匹配所有包含關鍵字且以數字結尾且該數字不是'0.0'(這是正則表達式的(?!0\.0)部分)的所有內容。

正則表達式在循環中執行,因爲它每次迭代匹配一行。

function getLines(text) { 
 
    var lines = []; 
 
    var re = /.*?(runtime|info|myfiles|css|assets|js).*?(?!0\.0)\d+\.?\d+/g; 
 
    while (m = re.exec(text)) { 
 
    lines.push(m[1]); 
 
    }; 
 

 
    return lines; 
 
} 
 

 
$(function() { 
 
    $('#printlogs').html(getLines($('textarea').val()).join('<br>')); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<textarea style="width:100%;height:120px"> 
 
error: there were errors found during runtime 12.0 
 
warning: incorrect info 1.0 
 
warning: file at 'C://localhost/myfiles' not found 0.0 
 
warning: file at 'C://localhost/css' not found 0.0 
 
warning: file at 'C://localhost/js' not found 0.0 
 
warning: file at 'C://localhost/assets' not found 5615612.0 
 
</textarea> 
 

 
<h4>Error Log</h4> 
 
<div id="printlogs" style="color:red"></div>

後面的代碼

getLines().forEach(function(line) { 
    if (line === 'info') { 
     // do your logic 
    } 
    if (line === 'js'||line ==='css'||line==='assets') { 
     // do your logic 
    } 
}); 
+0

我從來沒有見過這種正則表達式。它是如何工作的? ***?(?!0 \ .0)\ d + \。?\ d +/g; ** – learningbyexample

+0

我可以在parentesis ** runtime | info | myfiles | css | assets | js **中使用多個單詞?like(assets 1 | runtime local | js home files)將會是一個例子 – learningbyexample

+0

@learningbyexample yes,你可以 – venimus

0
var thestr = "warning: file at 'C://localhost/assets' not found 5615612.0"; 
var voila = thestr.substr(thestr.indexOf("found") + 5); //+ 6 to remove the leading space 
console.log(thestr); 
console.log(thestr.indexOf("found")); 
console.log(voila); 

這裏是你如何在 「發現」 分割字符串,但它的氣味。

我會編寫錯誤代碼並將其添加到日誌行中的可識別位置 - 當然,如果您是選擇日誌中的內容的人員。即

0001::warning: whatever text 

然後,你可以分析每一行的第一個字符,或分割線「::」,要知道,你遇到了「0001」的錯誤。現在在行尾應用一個類似的輕鬆解析方法。

相關問題