Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript从字符串中提取数字?_Javascript - Fatal编程技术网

如何使用javascript从字符串中提取数字?

如何使用javascript从字符串中提取数字?,javascript,Javascript,例如,我试图从字符串中提取数字 East Texas Baptist University (582) (COL) North Central Texas Academy (202471) (COL) Bloomfield College (1662) (COL) 我使用了parseInt,但它给了我NAN。有谁能提出更好的办法吗。谢谢试试这个: function getNumber(str) { return parseInt(str.match(/\d+/)[0], 10);

例如,我试图从字符串中提取数字

East Texas Baptist University (582) (COL)

North Central Texas Academy (202471) (COL)

Bloomfield College (1662) (COL)
我使用了parseInt,但它给了我NAN。有谁能提出更好的办法吗。谢谢

试试这个:

function getNumber(str) {
    return parseInt(str.match(/\d+/)[0], 10);
}
您可以使用如下功能:

var num = getNumber('East Texas Baptist University (582) (COL)');

您可以使用regex进行如下操作:

"Bloomfield College (1662) (COL)".match(/(\d+)/)[0] //1662

字符串是否始终采用这种格式?如果是这样,您可以使用regex..您可以使用regexp;)可能重复-可能重复的Yup字符串将始终使用此格式