【问题标题】:Get only one match with regexp只用 regexp 获得一场比赛
【发布时间】:2013-02-18 16:42:24
【问题描述】:

在下面的函数中,我遍历了一个包含字符串的数组(事件)。字符串描述了从另一个网络应用程序中提取的事件(犯罪或事故),而我正在做的是划分和计算不同的犯罪/事故并将它们放在一个对象中(INCIDENT_MATCHES)。

但是,某些文本字符串可能包含我搜索的几个关键字(例如“gunfire”和“battery”),但我不想要。相反,我只想计算第一个找到的单词,如果找到更多关键字,则应忽略它们。

这是怎么做到的?

var INCIDENT_MATCHES = {
    battery: /\w*(bråk)\w*|överfall|slagsmål|slogs|misshandel|misshandlad|\w*(tjuv)\w*/ig,
    burglaries: /snattade|snattare|snatta|inbrott|bestulen|stöld|\w*(tjuv)\w*/ig,
    robberies: /\w*(rån)\w*|personrån|\w*(ryckning)\w*|väskryckt*/ig,
    gunfire: /skottlossning|skjuten|sköt/ig,
    drugs: /narkotikabrott/ig,
    vandalism: /skadegörelse|klotter|\w*(klottra)\w*/ig,
    trafficAccidents: /(trafik|bil)olycka|(trafik|bil)olyckor|\w*(personbil)\w*|singelolycka|kollision|\w*(kollidera)\w*|påkörd|trafik|smitningsolycka/ig,
};

var j = 0,
incidentCounts = {},
incidentTypes = Object.keys(INCIDENT_MATCHES);

incidents.forEach(function(incident) {
    matchFound = false;

    incidentTypes.forEach(function(type) {
        if(typeof incidentCounts[type] === 'undefined') {
            incidentCounts[type] = 0;
        }
        var matchFound = incident.match(INCIDENT_MATCHES[type]);

        if(matchFound){
            matchFound = true;
            incidentCounts[type] += 1;
        }
    });

    j++;
});

【问题讨论】:

    标签: javascript regex object loops iterator


    【解决方案1】:

    您可以从“每个”处理程序返回 false 以停止迭代。

        if(matchFound){
            matchFound = true;
            incidentCounts[type] += 1;
            return false;
        }
    

    edit - 你会想要(我认为)在外循环结束时进行另一个测试:

      j++; // I don't understand what that does ...
      if (matchFound) return false;
    

    【讨论】:

    • 这实际上并没有因为某种原因停止计数。即使找到匹配项,也会再次检查相同的字符串是否匹配。
    • @holyredbeard 嗯,也许你需要另一个 return false; 用于外部“循环”(.each()) - 我会更新答案。
    • 我对您的解决方案进行了试验,发现将第二个“forEach”语句更改为“every”,将“return false”放入 if(matchFound) 中,然后放入“else { return true; }”成功了!
    • 感谢您对“return false”的帮助! :)
    【解决方案2】:

    我在下面找到了这个解决方案。我所做的如下:

    1. 我将第二个 forEach 语句替换为“every”
    2. 将“return false”放入“if(matchFound)”
    3. 添加了“else { return true; }”以便在找不到匹配项时继续循环。

    代码:

    incidents[2].forEach(function(incident) {
        matchFound = false;
    
        incidentTypes.every(function(type) {
            if(typeof crimesPerType[type] === 'undefined') {
                crimesPerType[type] = 0;
        }
        var matchFound = incident.match(INCIDENT_MATCHES[type]);
    
        if(matchFound){
            crimesPerType[type] += 1;
            if (type == 'trafficAccidents') {
                incidents[3][j].push('traffic');
            }
            else {
                incidents[3][j].push('crime');
            }
            return false;
        }
        else {
            return true;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 2019-07-26
      • 2010-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多