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...
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)); }
下面的正则表达式应该解决它
values[3].replace(/\.+[a-z]*$/, '')
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"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.