angularjs - How to trim 0 in day but not in month with format(yyyy-dd-mm) in angular/javascript? -


how trim javascript date object. have date format (yyyy-d-m). date value 2015-03-03 , want trim first 0 in days not in months.

example: when input: 2015-03-03 returned 2015-3-03

i tried following code it's trim 0 in months.

 $scope.date = {      "start": "2015-01-26",      "end": "2015-04-03"  };    `var = $scope.date.from.replace(/\b0(?=\d)/g, '');    var = $scope.date.to.replace(/\b0(?=\d)/g, '');    console.log(to);    //output 2015-1-26 , 2015-4-3` 

what should do?

if want remove leading 0 day, then:

$scope.date.from.replace(/-0(\d)$/,'-$1') 

will do. there no need lookahead or g flag (since you're replacing 1 instance).

edit

sorry, should have explained how regular expression , replacement works.

in regular expression, -0(\d)$ matches dash, followed zero, followed digit, followed end of string. brackets ( ) capture matched digit in hold space represented in replacement string $1.

the whole match replaced replacement string '-$1' dash, followed matched digit: $1.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -