I want to recognize football matches with Regex in JavaScript:
1
15/06 16:00
Brasília
Brasilien
3:0 (1:0)
Japan
2
23/06 16:00
Recife
Uruguay
-
Tahiti
This text contains:
- Date and Time of the match
- The Place where the match is
- The two teams
- the score if the game is already played OR if not it contains a "-"
i have build a regex with http://regex101.com/ site:
(\d\d\/\d\d)\s(\d\d:\d\d)\s(.+)\s\s\s(.+)\s(?:-|(\d):(\d)\s\(\d:\d\))\s(.+)
This regex is should capture both alternatives(with score and without) Here is a link to the whole testing stuff: http://regex101.com/r/bF3lU4
My Code in JavaScript with NodeJS:
function CreateMatchesFromString(data)
{
var re = /(\d\d\/\d\d)\s(\d\d:\d\d)\s(.+)\s\s\s(.+)\s(?:-|(\d):(\d)\s\(\d:\d\))\s(.+)/g;
var myArray;
while ((myArray = re.exec(data)) !== null)
{
console.log("date:"+ myArray[1]);
console.log("time:"+ myArray[2]);
console.log("place:"+ myArray[3]);
console.log("Home:"+ myArray[4]);
console.log("Away:"+ myArray[5]);
}
}
But i not get the Away-Team which is the Capture Group 5! My Output:
date:26/06
time:22:00
place:Curitiba
Home:Algerien
Away:undefined
I get it only when i not make an alternative expression with "|":
(\d\d\/\d\d)\s(\d\d:\d\d)\s(.+)\s\s\s(.+)\s-\s(.+)
Or when i use "[" "]" instead of the "(" and ")" for grouping the alternatives.
What is the problem? Is it a a bug in Nodejs regex-engine because it ignores the last capture group!? Or is the Regex wrong?
Best Regards Michael