Subjective Effect Subjective Effect - 6 months ago 8
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 = Date.now() - timeStart; // total elapsed time in ms
$scope.date = new Date($scope.time);
$scope.preDisplay = $scope.date.toUTCString();
$scope.timeDisplay = $scope.preDisplay.match(/\d+:\d+:\d+/);


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

["00:00:00"]


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

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

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