I am trying to convert military time from a string with regex... what I have so far is
var a = "[18:37:56]";
var b = a.replace(/(\[|\])/g,'');
var c = b.match(/^[0-9][0-9]/);
var d = Number(c[0]) > 12 ? Number(c[0]) - 12 : Number(c[0]);
b.replace(/^[0-9][0-9]/,d);
The first replace is to get rid of the square brackets. Then we match the first two numbers, then after this we will see if that number is greater than 12, if so subtract 12 if not leave it alone. Then replace those numbers with the corresponding number.
Problem is for one, what if the for c there is no second number, meaning it's only at 12:01am or 9:59am in standard time. I'm seeing a lot of flaws that can come up with this, does anyone have any better solutions than what I've done?
18 % 12 === 6%for in terms of here.var c = b.match(/^[0-9]{1,2}/);to handle the single number issue