Subjective Effect Subjective Effect - 7 months ago 12
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?


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

$scope.timeDisplay = $scope.timeDisplay[0];