Regular expressions are worth learning for every front-end engineer — 1. Formatting money I often need to format money at work and using regex makes it really easy. const formatPrice = (price) => {
const regexp = new RegExp(`(?!^)(?=(\\d{3})+${price.includes('.') ? '\\.' : '$'})`, 'g')
return price.replace(regexp, ',')
}
formatPrice('123') // 123
formatPrice('1234') // 1,234
formatPrice('123456') // 123,456
formatPrice('123456789') // 123,456,789
formatPrice('123456789.123') // 123,456,789.123