What would the regex pattern be to match all decimals but the first one? I'm using javascript's replace(), and would like to remove all but the first decimal in a string.
Examples:
1.2.3.4.5 --> 1.2345
.2.3.4.5 --> .2345
1234.. --> 1234.
+: one or more ( 1+ ), e.g., [0-9]+ matches one or more digits such as '123' , '000' . *: zero or more ( 0+ ), e.g., [0-9]* matches zero or more digits. It accepts all those in [0-9]+ plus the empty string.
Regex101.com is an interactive regular expression console that lets you debug your expressions in real-time.
- a "dot" indicates any character. * - means "0 or more instances of the preceding regex token"
You could do something like this:
function parseAndNormalizeDecimal(dec) {
var i = 0;
var result = dec.replace(/\./g, function(all, match) { return i++===0 ? '.' : ''; });
return result;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With