2012-10-10 53 views
3

我有以下數據的陣列名爲cityList:JavaScript的正則表達式匹配城市名

var cityList = [ 

    "Anaa, French Polynesia (AAA)", 
    "Arrabury, Australia (AAB)", 
    "Al Arish, Egypt (AAC)", 
    "Ad-Dabbah, Sudan (AAD)", 
    "Annaba, Algeria (AAE)", 
    "Apalachicola, United States (AAF)", 
    "Arapoti, Brazil (AAG)", 
    "Aachen, Germany (AAH)", 
    "Arraias, Brazil (AAI)", 
    "Awaradam, Suriname (AAJ)", 
    "Aranuka, Kiribati (AAK)", 
    "Aalborg, Denmark (AAL)" 
]; 

我想先搜索城市的名字開始在字符串的開頭。

接下來我要搜索的字符串的代碼部分:AAA,AAB,AAC,等...

我想申請一個搜索模式爲JavaScript正則表達式,第一個城市名稱,第二個城市代碼。

這裏是我的正則表達式:

// this regular expression used for search city name 
var matcher = new RegExp("^" + re, "i"); 

// this regular expression used for search city code 
var matcher = new RegExp("([(*)])" + re, "i"); 

如何這兩個正則表達式組合成的作品所描述的一個正則表達式?

+0

'var matcher','var matcher' ?? – elclanrs

+0

第二個var匹配器行被評論。 – Viju

+0

你想完成什麼?看起來像訂單的另一個配方,在預期的客戶已經承諾進行購買後,在最後一步厭惡放棄,因爲錯誤的「驗證」嘗試拒絕完全有效的信息。 – tripleee

回答

1

我的建議是:

var myregexp = /^([^,]+),[^(]*\(([^()]+)\)/; 
var match = myregexp.exec(subject); 
if (match != null) { 
    city = match[1]; 
    code = match[2]; 
} 

說明:

^  # Start of string 
(  # Match and capture (group number 1): 
[^,]+ # One or more characters except comma (alternatively insert city name) 
)  # End of group 1 
,  # Match a comma 
[^(]* # Match any number of characters except an opening parenthesis 
\(  # Match an opening parenthesis 
(  # Match and capture (group number 2): 
[^()]+ # One or more characters except parentheses (alt. insert city code) 
)  # End of group 2 
\)  # Match a closing parenthesis 

這是假設沒有任何一個城市的名字將永遠包含逗號(,否則這個表達式將只捕獲逗號之前的部分) ,所以你需要檢查你的數據是否有可能。我想不出一個例子,但是,這不是說什麼:)

0

我想你想在幾個簡單的步驟來實現:

  1. 分割每個字符串數組中前後第一個括號

  2. 將第一個正則表達式應用到字符串的第一部分。存儲結果爲布爾變量,或許命名爲matchOne

  3. 應用你的第二個正則表達式的字符串的第二部分(不要忘記刪除右括號)。將結果存儲爲布爾變量,可能命名爲matchTwo

  4. 測試如果任一兩個MATHES的成功:return (matchOne || matchTwo);

0
$("#leavingCity").autocomplete({ 
source: function(req, responseFn) { 
      var re = $.ui.autocomplete.escapeRegex(req.term); 
      var matcher = new RegExp("/^([^,]+),[^(]*\(([^()]+)\)/", "g"); 
      var a = $.grep(cityList, function(item,index) { return matcher.test(item); }); 
      responseFn(a); 
} }); 

由Tim Pietzcker試試這個,regualr表達

+0

需要jQuery和自動完成插件。 –

0

這是最優雅的方式,我可以做到這一點:

var cityList = ["Anaa, French Polynesia (AAA)","Arrabury, Australia (AAB)","Al Arish, Egypt (AAC)","Ad-Dabbah, Sudan (AAD)","Annaba, Algeria (AAE)","Apalachicola, United States (AAF)","Arapoti, Brazil (AAG)","Aachen, Germany (AAH)","Arraias, Brazil (AAI)","Awaradam, Suriname (AAJ)","Aranuka, Kiribati (AAK)","Aalborg, Denmark (AAL)"]; 
var regex = /([a-z].+?),.+?\(([A-Z]{3,3})\)/gi, match, newList = []; 

while (match = regex.exec(cityList)) { 
    newList.push(match[1]+" - "+match[2]); 
} 

alert(newList[7]); 
// prints Aachen - AAH 

如果您不明白如何使用圓括號在你的正則表達式中,我建議你看看我學到的網站:http://www.regular-expressions.info/

0

這裏我建議一個完全不同的方法(ECMA-262標準)。

作爲使用正則表達式需要一個線性搜索,無論如何,如果你能預先處理數據,您可以設置城市對象的數組:

function City(name, country, code){ 
    this.cityName = name; 
    this.cityCountry = country; 
    this.cityCode = code; 
} 

var cities = []; 
cities.push(new City('Anaa', 'French Polynesia', 'AAA')); 
// ... push the other cities 

和搜索功能:

function GetCity(cityToSearch, cities){ 
    var res = null; 
    for(i=0;i<cities.length;i++){ 
    if(cities[i].city = cityToSearch 
     res = cities[i]; 
    } 
    return res; 
} 

在運行時:

var codeFound = ''; 
var cityFound = GetCity('Arraias'); 
if(cityFound != null) 
    codeFound = cityFound.cityCode; 

備註

在這兩種情況下,如果要填充城市陣列與世界上所有城市,城市名稱不是關鍵!例如在美國有六個'Spirngfield'。在這種情況下,更好的方法是使用雙字段密鑰。