Scenario: You have to find if the time string is in hh:mm am/pm format. This code will work even if the hour has only one digit.
Function:
function isValidTimeFormat(timeStr) {
const regex = /^([1-9]|1[0-2]):([0-5][0-9])s([APap][Mm])$/;
return regex.test(timeStr);
}
Result:
function will return true for both 5:00 am and 05:00 am values and will return false if the string is not in the above-mentioned format.