Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
138
rated 0 times [  143] [ 5]  / answers: 1 / hits: 15677  / 12 Years ago, mon, october 1, 2012, 12:00:00

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.




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.



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) //doesn't draw correctly
start = get100YardVersionEugene(V09) // draw correctly


More From » regex

 Answers
14

You can simplify your regex a little so it only checks for H or 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.



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];
}
}
},

[#82818] Saturday, September 29, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
freddiejarretk

Total Points: 612
Total Questions: 103
Total Answers: 88

Location: Armenia
Member since Sat, Dec 31, 2022
1 Year ago
;