Javascript 正则表达式匹配()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12675839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Regex Match()
提问by ealeon
I just want this function to return an int back based on a string but its not working It will be either H or V followed by 1 or 2 digits.
我只是想让这个函数根据字符串返回一个 int 但它不起作用它将是 H 或 V 后跟 1 或 2 位数字。
IE: H1 return 99 H09 return 91 H10 return 90 H50 return 50 V1 return 1 V05 return 5 V11 return 11 V50 return 50
spot will be my string thats going in.
现场将是我的字符串。
get100YardVersionEugene: function(spot)
{
var team = spot.match(/[A-Z]+/);
var yard = spot.match(/([0-9]+)/);
if (team == "H")
{
return 100-yard;
}
else //V
{
return yard;
}
},
for some reason when its V9(or H9) it breaks but when i put in V09 it works.
出于某种原因,当它的 V9(或 H9)损坏时,但当我放入 V09 时它可以工作。
Can someone tell me why?
有人能告诉我为什么吗?
EDIT: it breaks as in... I have two variables start and end
编辑:它打破了......我有两个变量开始和结束
so i have something like
start = get100YardVersionEugene("V9")
and I use start and end to draw it on html5 canvas
所以我有类似 start = get100YardVersionEugene("V9") 的东西
,我使用 start 和 end 在 html5 画布上绘制它
start = get100YardVersionEugene("V9") //doesn't draw correctly start = get100YardVersionEugene("V09") // draw correctly
start = get100YardVersionEugene("V9") // 绘制不正确 start = get100YardVersionEugene("V09") // 绘制正确
回答by I Hate Lazy
You can simplify your regex a little so it only checks for H
or V
.
您可以稍微简化一下正则表达式,以便它只检查H
或V
。
regarding the number, you need to remember that match
returns an Array, so you'll need to get the value by index. Also, you shouldn't need the capture group.
关于数字,您需要记住它match
返回一个数组,因此您需要通过索引获取值。此外,您不应该需要捕获组。
And actually, you should really just use one regex.
实际上,您真的应该只使用一个正则表达式。
get100YardVersionEugene: function(spot)
{
var parts = spot.match(/(H|V)([0-9]+)/);
if (parts) {
if (parts[1] == "H")
{
return 100-(+parts[2] || 0);
}
else //V
{
return +parts[2];
}
}
},
回答by gpojd
Try wrapping yard in [parseInt][1]
.
尝试将院子包裹在[parseInt][1]
.
var yard = parseInt(spot.match(/([0-9]+)/), 10);
回答by Denys Séguret
The result of the match function is an array. Do this :
匹配函数的结果是一个数组。做这个 :
var team = spot.match(/[A-Z]+/)[0];
And you also need to parse the result as int.
并且您还需要将结果解析为 int。
Alternatively, it doesn't seem like regex are really needed here :
或者,这里似乎并不真正需要正则表达式:
var team = spot.substring(0, 1);
var yard = parseInt(spot.substring(1), 10);
return team=='H' ? (100-yard) : yard;
回答by PiTheNumber
regexp is total overkill for this. Try String.charAt() or String.substr():
正则表达式对此完全矫枉过正。试试 String.charAt() 或 String.substr():
get100YardVersionEugene: function(spot)
{
var team = spot.charAt(0);
var yard = parseInt(spot.substr(1,2), 10);
...
}
回答by Seb T.
You probably need to convert your yard into number
您可能需要将院子转换为数字
if( team == H ){
return 100-parseInt(yard);
}
Then might be good to check for convertion validity using isNaN method.
然后使用 isNaN 方法检查转换有效性可能会很好。
Hope this helps.
希望这可以帮助。
回答by Anoop
Modified code: spot.match(/([0-9]+)/)
is returing an array
修改后的代码:spot.match(/([0-9]+)/)
正在返回一个数组
function get100YardVersionEugene(spot)
{
var team = spot.match(/[A-Z]+/);
var yard = spot.match(/([0-9]+)/);
console.log(team , yard );
if (team == "H")
{
return 100-yard[0];
}
else //V
{
return yard[0];
}
}