javascript - Why does this regex match on angular fail only on Safari? -
i have following code on angular project. chrome , firefox works in safary causes , exception.
var shour = "9:00:00 pm cdt"; var ehour = "12:00:00 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); // answer should 09:00pm console.log("ehour: " + conver_ehour); // answer should 12:00am
i try run on jsbin, plunkr , jsfiddle fail , cannot see cause.
this exception error: null not 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 appreciated. thanks
the regex:
/[ap][m]$/
is looking find am
or pm
@ end of string... characters don't appear @ end of string, match
returns null
. trying null[0]
throws exception.
you meant use:
/[ap]m/
var shour = "9:00:00 pm cdt"; var ehour = "12:00:00 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); // answer should 09:00pm console.log("ehour: " + conver_ehour); // answer should 12:00am
Comments
Post a Comment