Recently I posted a question about time format conversion via regular expressions in JS.
Now I modified the code a bit.
function getHours(value) {
if (value == 0)
return 0;
var re = new RegExp("^(?=\d)((\d+)(h|:))?\s*((\d+)m?)?$", "g");
var myArray = re.exec(value);
var hours = 0;
var minutes = 0;
if (myArray != null) {
if (myArray[2] != null) {
hours = myArray[2];
}
if (myArray[5] != null) {
minutes = myArray[5];
}
}
return Number(hours) + Number(minutes) / 60;
}
The problem is that it returns a null value in myArray.
As I'm new to JS, I couldn't solve this problem. What am I doing wrong?
valuevariable too{"11h20m","11h","20","20m","11:20"}(?=\d)do? There isx(?=y)which meansmatches x only if x is followed by ybut then you miss the preceding value.\d+(one ore more digits) as first value, so it already must start with a digit.