Javascript 使用regexp仅获取一个匹配项
在下面的函数中,我遍历一个数组(事件),该数组包含一组字符串。字符串描述的是从另一个web应用程序中删除的事件(犯罪或事故),我所做的是划分和计算不同的犯罪/事故,并将它们放在一个对象中(事件匹配) 但是,一些文本字符串可能包含我搜索的几个关键字(例如“gunfire”和“battery”),但我不希望这样。相反,我只想计算第一个找到的单词,如果找到更多的关键字,它们应该被忽略 这怎么可能呢Javascript 使用regexp仅获取一个匹配项,javascript,regex,object,loops,iterator,Javascript,Regex,Object,Loops,Iterator,在下面的函数中,我遍历一个数组(事件),该数组包含一组字符串。字符串描述的是从另一个web应用程序中删除的事件(犯罪或事故),我所做的是划分和计算不同的犯罪/事故,并将它们放在一个对象中(事件匹配) 但是,一些文本字符串可能包含我搜索的几个关键字(例如“gunfire”和“battery”),但我不希望这样。相反,我只想计算第一个找到的单词,如果找到更多的关键字,它们应该被忽略 这怎么可能呢 var INCIDENT_MATCHES = { battery: /\w*(bråk)\w*|
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++;
});
您可以从“each”处理程序返回
false
,以停止迭代
if(matchFound){
matchFound = true;
incidentCounts[type] += 1;
return false;
}
编辑-并且(我认为)您将需要在外部循环结束时,在该测试之外的另一个测试:
j++; // I don't understand what that does ...
if (matchFound) return false;
您可以从“each”处理程序返回
false
,以停止迭代
if(matchFound){
matchFound = true;
incidentCounts[type] += 1;
return false;
}
编辑-并且(我认为)您将需要在外部循环结束时,在该测试之外的另一个测试:
j++; // I don't understand what that does ...
if (matchFound) return false;
我在下面找到了这个解决方案。我所做的是:
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;
}
});
我在下面找到了这个解决方案。我所做的是:
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;
}
});
这实际上并没有因为某种原因而停止计数。即使在找到匹配项之后,也会再次检查相同字符串的匹配项。@holyredbeard啊,也许您需要另一个
返回false代码>对于外部“循环”(.each()
)-我将更新答案。我对您的解决方案进行了实验,发现将第二个“forEach”语句更改为“every”,在if(matchfind)中放入“return false”,然后放入“else{return true;}”就成功了!感谢您对“返回错误”的帮助!:)这实际上并没有因为某种原因而停止计数。即使在找到匹配项之后,也会再次检查相同字符串的匹配项。@holyredbeard啊,也许您需要另一个返回false代码>对于外部“循环”(.each()
)-我将更新答案。我对您的解决方案进行了实验,发现将第二个“forEach”语句更改为“every”,在if(matchfind)中放入“return false”,然后放入“else{return true;}”就成功了!感谢您对“返回错误”的帮助!:)