Pat
Pat

Reputation: 51

Javascript - Removing leading zeros from a string of negative number

I have this string "00-12.50" and I would like to remove the leading zeros with and replace with a blank space. How do I do that?

My Regex example is not working: strDedAmount.replace(/^[0]*/, ' ')

This example returns " 0-12.50" Thanks

Upvotes: 0

Views: 211

Answers (1)

sudavid4
sudavid4

Reputation: 1131

I suppose using the "with callback" version of string.replace(regex, callback) will serve you well

const str = '00-12.50'
const fixed = str.replace(/^0+/, match => ' '.repeat(match.length))
console.log(fixed)

Upvotes: 1

Related Questions