parseInt 不起作用,从字符串中获取数字

parseInt not working, get the number from the string

本文关键字:获取 数字 字符串 不起作用 parseInt      更新时间:2023-09-26

我正在尝试从字符串中获取数字,请参阅下面的代码片段,我尝试"parseInt"但不幸的是不起作用,请提供任何帮助,想法?

var tt = "test 12";
console.log(parseInt(tt));

parseInt()不起作用,因为tt不是以数字开头的。

您可以将match()与正则表达式一起使用,从字符串中提取数字。

var num = tt.match(/'d+/)[0];