I am trying to write a function which will parse a string in the form of 7:55am - 9:10am (TR) 8:10am - 9:00am (W) and output a multi dimensional ]array containing the regex result (ex [[7:55am, 9:10am, TR], [7:55am, 9:00am, W]])
This is my function as it currently stands:
function parseClassTime(times) {
console.log("Input: " + times);
var timeArr = [];
var finalArr = [];
regexStr = /(\d{1,2}:\d{2}[ap]m) - (\d{1,2}:\d{2}[ap]m) \((\w{1,5})\)/g;
if (times.indexOf(") ") > -1) {
times = times.replace(") ", ")&");
timeArr = times.split("&");
} else {
timeArr.push(times);
}
console.log("timeArr: " + timeArr);
for (i = 0; i < timeArr.length; i++) {
console.log(i + ":" + timeArr[i]);
console.log("regexResult: " + regexStr.exec(timeArr[i]));
}
};
And this is the output that I am getting:
Input: 7:55am - 9:10am (TR) 8:10am - 9:00am (W)
timeArr: 7:55am - 9:10am (TR),8:10am - 9:00am (W)
0:7:55am - 9:10am (TR)
regexResult: 7:55am - 9:10am (TR),7:55am,9:10am,TR
1:8:10am - 9:00am (W)
regexResult: null
For the life of me I cannot discover where that null is coming from. Does something happen to the regex string between the two calls to .exec()?
Please let me know your thoughts!
{0,5}to{1,5}. I think it's matching for zero length too.amandpmdangling, use[ap]m, then - as the comment above states -{0,5}can match 0 characters. Try(\d{1,2}:\d{2}[ap]m) - (\d{1,2}:\d{2}[ap]m) \((\w+)\).