I am trying to use Regex to match zero, followed by a period and exactly 6 digits. Such as โ 0.274538
6 digits can be any number 0-9. In addition, any symbols (including spaces) may exist before or after the match.
I would like to trim the tail 4 digits from the match, so the result is zero, and then 2 decimal places (i.e. 0.27).
I use this regular expression inside the Javascript string replacement function. I turn on the "g" modifier, as there will be several matches in the line.
I am not very good at regular expression, but I think the following code is pretty close to what I need ...
var replaced = string.replace(/[0]\.[0-9]{6}$/g, X);
Only ... I'm not sure what to use for the "replace" value here (X). If only one match was expected, then I could just use a โsliceโ on the replaced string. But since I expect some regular expressions in a string, what should I use for the value of "X"? Or is there a better way to do this?
source
share