Reputation:
I have the following code on my angular project. With Chrome and Firefox works well but in Safary causes and exception.
var shour = "9:00:00 PM CDT";
var ehour = "12:00:00 AM CDT";
var conver_shour = shour.match(/^(\d+):(\d+)/)[0] + shour.match(/[AP][M]$/)[0];
var conver_ehour = ehour.match(/^(\d+):(\d+)/)[0] + ehour.match(/[AP][M]$/)[0];
console.log("shour: " + conver_shour); // The answer should be 09:00PM
console.log("ehour: " + conver_ehour); // The answer should be 12:00AM
I try to run on jsbin, plunkr and jsfiddle but something fail and I cannot see what is the cause.
This is the exception Error: null is not an object (evaluating 'shour.match(/[AP][M]$/)') $eval@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js:142:467 $apply@https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js:143:193 https://cdnjs.cloudflare.com/ajax/libs/angular-ui-calendar/1.0.0/calendar.min.js:1:326 https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js:156:171
Any help is appreciated. Thanks
Upvotes: 0
Views: 395
Reputation: 33516
The regex:
/[AP][M]$/
Is looking to find AM
or PM
at the end of the string... but those characters don't appear at the end of the string, so match
returns null
. Trying to do null[0]
throws the exception.
You probably meant to use:
/[AP]M/
var shour = "9:00:00 PM CDT";
var ehour = "12:00:00 AM CDT";
var conver_shour = shour.match(/^(\d+):(\d+)/)[0] + shour.match(/[AP]M/)[0];
var conver_ehour = ehour.match(/^(\d+):(\d+)/)[0] + ehour.match(/[AP]M/)[0];
console.log("shour: " + conver_shour); // The answer should be 09:00PM
console.log("ehour: " + conver_ehour); // The answer should be 12:00AM
Upvotes: 0