[英]Javascript Regex Match string
函数中的输入或点(总是沿线显示):
华盛顿T.冲3码到MT0
我想获取文本“ to the MT0
”
“ MT”将与在函数调用之前初始化的var homeacrynm
或awayacrynm
匹配。 到目前为止,这是我尝试过的:
getEndSpotEugene: function(spot)
{
var regex = new RegExp("to the +("+homeacrynm+"|"+awayacrynm+")?([0-9]{1,2})","g");
var matches = spot.match(regex);
if (matches)
{
pos = matches[matches.length-1]
matches = pos.match(/to the ([A-Z]+)?([0-9]{1,2})/);
if (!matches[1])
{
matches=[pos,"V",50];
}
}
else
{
return -1;
}
var acr = matches[1];
var yard = matches[2];
if (acr == homeacrynm)
return "H"+yard;
else
return "V"+yard;
},
例如(一种简单情况):
homeacrynm = "MT"
var giveMe = getEndSpotEugene("Washington, T. rush for 3 yards to the MT11")
giveMe
应该是H11,但出于某种原因而不是。
我也不太清楚哪里错了。 你们看到我想念的东西吗? 谢谢!
我制作了一些日志,并明确声明了homeacrynm和awayacrynm,如下所示:
var homeacrynm = "MT";
var awayacrynm = "H";
var getEndSpotEugene = function(spot)
{
var regex = new RegExp("to the +("+homeacrynm+"|"+awayacrynm+")?([0-9]{1,2})","g");
console.log(regex);
var matches = spot.match(regex);
console.log(matches);
if (matches)
{
pos = matches[matches.length-1]
matches = pos.match(/to the ([A-Z]+)?([0-9]{1,2})/);
if (!matches[1])
{
matches=[pos,"V",50];
}
}
else
{
return -1;
}
var acr = matches[1];
var yard = matches[2];
console.log(acr);
console.log(yard);
if (acr == homeacrynm)
return "H"+yard;
else
return "V"+yard;
}
奇怪的是,我得到了您期望的H11! 你得到什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.