Subjective Effect Subjective Effect - 1 year ago 61
Javascript Question

Why does this regular expression give these extra characters?

I'm making a timer that counts up. A base time is created and then the difference between now and the base time is calculated.

I then make that difference a date, turn it into a string and extract the time in seconds, minutes and hours from it.

Code looks like this:

$scope.time = - timeStart; // total elapsed time in ms
$ = new Date($scope.time);
$scope.preDisplay = $;
$scope.timeDisplay = $scope.preDisplay.match(/\d+:\d+:\d+/);

The problem is $scope.timeDisplay shows up looking like this:


I don't get where the square brackets and quotation marks are coming from. This is in angularjs, if I use JS or JQuery they aren't there.

What is going on?

Answer Source

The matches (.match) return an array of matching elements. Try accessing it using the first 0th index:

$scope.timeDisplay = $scope.timeDisplay[0];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download