2016-11-14 93 views
1

我想在javascript中使用像sql這樣的子句。無法找到一個字符串是否包含子字符串?

據我所知,類似的問題已經存在互聯網上,但下面的方法是不是爲我工作:

$(document).ready(function() { 
 
    var listAll = [ 
 
    "X1", 
 
    "ANTENNA SYSTEMS          00000000AS", 
 
    "Security & Wrokf         00000000CS", 
 
    "MICROWAVE & COMM         00000000MC", 
 
    "MICROWAVE SENSOR         00000000MT", 
 
    "PLANNING & PROJE         00000000PG", 
 
    "MECHANICAL SYSTE         00000000MS", 
 
    "ELECTRO-OPTICAL          00000000EO", 
 
    "SATCOM EXPERIMEN         00000000SE", 
 
    "QUALITY ASSURANC         00000000QA", 
 
    "QUALITY ASSURANC         00000000QC", 
 
    "DATA PRODUCTS SO         00000000DP" 
 
    ]; 
 
    var lstfiltered = ["X2"]; 
 

 
    for (i = 0; i <= listAll.length - 1; i++) { 
 
    console.log(listAll[i]); 
 
    var string = listAll[i]; 
 
    var substring = "lan"; 
 
    if (string.indexOf(substring) !== -1) { 
 
     lstfiltered.push(string); 
 
    } 
 
    } 
 

 
    console.log(lstfiltered); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

我已經設置子是在字符串中查找作爲「lan」,應該將「PLANNING & PROJE 00000000PG」插入數組中。但事實並非如此。

+3

區分大小寫?嘗試在比較之前先改爲小寫? – guradio

+1

@guradio [是的](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf#Case-sensitivity)。 –

+0

'toLowerCase()'使它不區分大小寫,或者你可以使用'localeCompare()'來比較字符串 –

回答

2

我認爲問題出在信封上。試試這個:

$(document).ready(function() { 
 
    var listAll = ["X1", "ANTENNA SYSTEMS          00000000AS", "Security & Wrokf         00000000CS", "MICROWAVE & COMM         00000000MC", "MICROWAVE SENSOR         00000000MT", "PLANNING & PROJE         00000000PG", "MECHANICAL SYSTE         00000000MS", "ELECTRO-OPTICAL          00000000EO", "SATCOM EXPERIMEN         00000000SE", "QUALITY ASSURANC         00000000QA", "QUALITY ASSURANC         00000000QC", "DATA PRODUCTS SO         00000000DP"]; 
 
    var lstfiltered = ["X2"]; 
 
    
 
    for (i = 0; i <= listAll.length - 1; i++) { 
 
    console.log(listAll[i]); 
 
    var string = listAll[i]; 
 
    var substring = "lan"; 
 
    if (string.toLowerCase().indexOf(substring) !== -1) { 
 
     lstfiltered.push(string); 
 
    } 
 
    } 
 

 
    console.log(lstfiltered); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

+0

愚蠢的信封 – Arbaaz

+0

該死的,我知道。我們需要注意的是將其轉換成小寫或大寫進行比較。 –

1

我建議尋找小寫字母和移動搜索字符串的循環之外。

$(document).ready(function() { 
 
    var listAll = ["X1", "ANTENNA SYSTEMS          00000000AS", "Security & Wrokf         00000000CS", "MICROWAVE & COMM         00000000MC", "MICROWAVE SENSOR         00000000MT", "PLANNING & PROJE         00000000PG", "MECHANICAL SYSTE         00000000MS", "ELECTRO-OPTICAL          00000000EO", "SATCOM EXPERIMEN         00000000SE", "QUALITY ASSURANC         00000000QA", "QUALITY ASSURANC         00000000QC", "DATA PRODUCTS SO         00000000DP"]; 
 
    var lstfiltered = ["X2"]; 
 
    var substring = "lan"; 
 
    
 
    for (i = 0; i <= listAll.length - 1; i++) { 
 
    console.log(listAll[i]); 
 
    var string = listAll[i]; 
 
    if (string.toLowerCase().indexOf(substring) !== -1) { 
 
     lstfiltered.push(string); 
 
    } 
 
    } 
 

 
    console.log(lstfiltered); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

對於一個更簡潔的版本,你可以使用Array#filter

$(document).ready(function() { 
 
    var listAll = ["X1", "ANTENNA SYSTEMS          00000000AS", "Security & Wrokf         00000000CS", "MICROWAVE & COMM         00000000MC", "MICROWAVE SENSOR         00000000MT", "PLANNING & PROJE         00000000PG", "MECHANICAL SYSTE         00000000MS", "ELECTRO-OPTICAL          00000000EO", "SATCOM EXPERIMEN         00000000SE", "QUALITY ASSURANC         00000000QA", "QUALITY ASSURANC         00000000QC", "DATA PRODUCTS SO         00000000DP"]; 
 
    var substring = "lan"; 
 
    var lstfiltered = listAll.filter(function (a) { 
 
      return a.toLowerCase().indexOf(substring) !== -1; 
 
     }); 
 

 
    lstfiltered.unshift('X2') 
 
    console.log(lstfiltered); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

0

如果你想要把原來的字符串,然後用繩子比較使用轉換toLowerCase()並將原始字符串推送爲sho wn如下:

$(document).ready(function() { 

    var listAll = ["X1", "ANTENNA SYSTEMS   00000000AS", "Security & Wrokf   00000000CS", "MICROWAVE & COMM   00000000MC", "MICROWAVE SENSOR   00000000MT", "PLANNING & PROJE   00000000PG", "MECHANICAL SYSTE   00000000MS", "ELECTRO-OPTICAL   00000000EO", "SATCOM EXPERIMEN   00000000SE", "QUALITY ASSURANC   00000000QA", "QUALITY ASSURANC   00000000QC", "DATA PRODUCTS SO   00000000DP"]; 

    var lstfiltered = ["X2"]; 




    for (i = 0; i <= listAll.length - 1; i++) { 
    console.log(listAll[i]); 

    var string = listAll[i].toLowerCase(); 

    var substring = "lan"; 
    if (string.indexOf(substring) !== -1) // compare with string converted by toLowerCase() 
     { 

     lstfiltered.push(listAll[i]); // Push original string. 

    } 
    } 

    console.log(lstfiltered); 


}); 
相關問題