简体   繁体   English

NodeJS从字符串中提取数字

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM