this is a continuation of How do I use regex to replace periods in a string?
for context I have a large table with strings containing dimensions
ie. " .175" x 2.5" x 8' "
// " means inches, ' means feet
I need to split every string into columns thickness, width and length. The solution I was provided in the previous example:
let x = "0.75 x 0.75 x 5.25 (144 bar)";
let numbers = x.match(/(d*.)?d+/g).map(n => parseFloat(n))
var thickness = numbers[0]
var width = numbers[1]
var length = numbers[2]
//Do the regex function
const regexFun = () => {
try {
console.log(x);
console.log(thickness, width, length);
} catch (err) {
console.log(err.message)
}
}
The above will only really work 70% of the time for me due to how some entries are written
ie. " 72" extrusion "
Ideally there is a single line solution out there, but I don’t quite understand how regex works:
/(d*.)?d+/g
In the above how does it return .175
, I would assume it finds a number first and then the dot, so the result would be 175.