I want to replace with a regex any non digit character and the dots after the first one so
"--r43dsd.32.32" would be 43.3232
"--r4.3dsd.32.3.2" would be 4.33232
"--r43dsd.32.3.2" would be 43.3232
I tried this
.replace(/[^0-9.]/g, '')
but I don't know how to remove the dots with replace and a regex. I read that I should use regex look ahead but I did not understand how to do it.
Is this achievable with only a regex and replace? I also created a function but I want to do it in a more clean way with a regex.
CodePudding user response:
I know it's not a regex, but you could always do something like that:
function getFloat(ourString) {
return ourString.replace(/[^0-9.]/g, '').substring(0, ourString.indexOf('.') 1) ourString.substring(ourString.indexOf('.') 1).replaceAll('.', '');
}
CodePudding user response:
Here is a regex based approach, using two steps. First, we can strip away all characters which are not either digits or dot. Then, using a callback function, we can remove dots other than the first occurrence.
var inputs = ["--r43dsd.32.32", "--r4.3dsd.32.3.2", "--r43dsd.32.3.2"];
for (var i=0; i < inputs.length; i) {
var output = inputs[i].replace(/[^0-9.] /g, "")
.replace(/([0-9] \.)(.*)/,
(m,n,o) => n o.replace(/[.]/g, ""));
console.log(output);
}
CodePudding user response:
let value = '"--r43dsd.32.32" would be 43.3232', i = 0;
value = value.replace(/[^0-9.]/g, '');
const result1 = value.replace(/[\.\%]/g, function(match) {
return match === "." ? (i === 0 ? '.' : '') : '';
});
// Here is a self-contained version with no external variables:
const result2 = value.replace(/[\.\%]/g, function(match, offset, all) {
return match === "." ? (all.indexOf(".") === offset ? '.' : '') : '';
});
console.log('result1', result1);
console.log('result2', result2);
CodePudding user response:
Don't use the global attribute to find only the first ., substitute it for x, change all the other .s to the empty string, and finally substitute a . for the x.
s1="--r43dsd.32.32"; // 43.3232
s2="--r4.3dsd.32.3.2"; // 4.33232
s3="--r43dsd.32.3.2"; // 43.3232
s=[s1,s2,s3];
s.forEach((s)=>{s=s.replace(/[^0-9.]/g, '').replace(/\./, 'x').replace(/\./g, '').replace(/x/, '.');console.log(s);});
CodePudding user response:
In this case you can use regex capture groups to achieve your objective. Anything before the first '.' can be called the prefix and everything after it can be called suffix. Then remove every non digit from prefix and suffix and concatenate the string.
let exp = /^(?<prefix>.*?)\.(?<suffix>.*)$/;
let input = "--r4.3dsd.32.32";
let groups = input.match(exp).groups;
console.log("prefix:", groups.prefix); // --r4
console.log("suffix:", groups.suffix); // 3dsd.32.32
// To get your required answer, simply replace non digits from both prefix and suffix and join the results:
console.log(groups.prefix.replace(/[\D] /g, '') '.' groups.suffix.replace(/[\D] /g, '')) // 4.33232
CodePudding user response:
Plenty of answers to pick from but I see they all concentrate on one or more replacements. However, you could do this in a singel replacement:
(?<=\..*)\.|[^\d.\n]
See an online demo
const regex = /(?<=\..*)\.|[^\d.] /g;
[
"--r43dsd.32.32",
"--r4.3dsd.32.3.2",
"--r43dsd.32.3.2"
].forEach(s =>
console.log(s.replace(regex, ''))
)
