For example:
"10.0.cm" goes to "10.0"
and
"10.0.m" goes to "10.0"
and
"3" stays as "3"
etc...
I tried this:
values[3].replace(/[^0-9.,]+/, '')
but this still leaves the "." after the number, eg: 10.0.
Thanks for your help...
For example:
"10.0.cm" goes to "10.0"
and
"10.0.m" goes to "10.0"
and
"3" stays as "3"
etc...
I tried this:
values[3].replace(/[^0-9.,]+/, '')
but this still leaves the "." after the number, eg: 10.0.
Thanks for your help...
The following regex should solve it
values[3].replace(/\.+[a-z]*$/, '')
var values = ["10.0.cm", "10.0.m", "3"];
var patten = /\d+(\.\d+)?/g;
for (var i = 0; i < values.length; i++) {
console.info(values[i].match(patten));
}
2 Array which contains two .s if given a String like 'foo_1.0.bar.1.2.xyz'The problem you've described isn't a simple "find numbers in a String"
It's not easy to do this in one step because RegExp in JavaScript doesn't support lookbacks. However
.var str = 'foo_1.0.bar.1.2.xyz';
str = str.replace(/[^\d\.]/g, '').split('.');
str = str[0] ? str[0] + '.' + str.slice(1).join('') : '';
str; // "1.012"