javascript正则表达式匹配cityname

时间:2012-10-10 06:01:28

标签: javascript regex

我有以下名为cityList的数据数组:

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

如何将这两个正则表达式组合成一个按照描述工作的正则表达式?

6 个答案:

答案 0 :(得分:2)

我建议:

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

这假设没有城市名称将包含逗号(否则此正则表达式只捕获逗号之前的部分),因此如果可能,您需要检查数据。我想不出一个例子,但那并没有说什么:)

答案 1 :(得分:1)

$("#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的正则表达式

答案 2 :(得分:1)

这是我能做到的最优雅的方式:

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/

答案 3 :(得分:1)

在这里,我建议采用完全不同的方法(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;

<强>备注

在这两种情况下,如果您打算将城市阵列填充到世界上所有城市,那么城市名称不是关键!例如,美国有六个“斯普林菲尔德”。在这种情况下,更好的方法是使用双字段密钥。

答案 4 :(得分:0)

我想你想通过几个简单的步骤来实现这个目标:

  1. 在第一个括号

  2. 之前和之后拆分数组中的每个字符串
  3. 将第一个正则表达式应用于字符串的第一部分。将结果存储为布尔变量,可能名为 matchOne

  4. 将第二个正则表达式应用于字符串的第二部分(不要忘记删除右括号)。将结果存储为布尔变量,可能名为 matchTwo

  5. 测试两个mathes中的任何一个是否成功:return ( matchOne || matchTwo );

答案 5 :(得分:0)

使用indexOf

它更加有效和明确。正则表达式是不必要的。

const isMatchX = cityList.indexOf('AAB');
const isMatchY = cityList.indexOf('Awar');

或者,您可以这样做,但是当您可以使用indexOf时,它的方法会过大

const search = (cityList, re) => {
  const strRegPart1 = "¬[^¬]*" + re + "[^¬]*";
  const strRegPart2 = "¬[^¬]*\\([^\\)]*" + re + "[^\\)]*\\)($|¬)";
  const regSearch = RegExp("(" + strRegPart1 + "|" + strRegPart2 + ")", "gi");
  const strCityListMarked = '¬' + cityList.join('¬');
  const arrMatch = strCityListMarked.match(regSearch);
  return arrMatch && arrMatch[1].substr(1);
}