[英]NodeJS extract a number from a String
I have this string wn123456 kg
for example 例如,我有这个字符串
wn123456 kg
I would like to take out only the numbers in this case: 123456
在这种情况下,我只想取出数字:
123456
Use this one - /\\d+/g
. 使用这个-
/\\d+/g
。 It will extract the number. 它将提取数字。
const str = 'wn123456 kg'; console.log(/\\d+/g.exec(str)[0]);
var str = 'wn123456 kg';
var num = str.replace(/\D/g, '');
console.log(num);
You can do this: using match
您可以这样做:使用
match
var text = 'wn123456 kg'; var number = text.match(/\\d+/); alert(number);
using replace
使用
replace
alert('wn123456 kg'.replace(/\\D/g, ''));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.