简体   繁体   中英

How to extract number from string using javascript?

I am to trying to extract numbers from string for example

East Texas Baptist University (582) (COL)

North Central Texas Academy (202471) (COL)

Bloomfield College (1662) (COL)

I have used parseInt but it gives me NAN. Can any one please suggest a better way. Thanks

您可以像这样使用正则表达式:

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

Try this:

function getNumber(str) {
    return parseInt(str.match(/\d+/)[0], 10);
}

You can use the function like this:

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

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.

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