0

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

k-nut
  • 3,447
  • 2
  • 18
  • 28
Ahmad
  • 35
  • 7

2 Answers2

4

You can use regex for that like:

"Bloomfield College (1662) (COL)".match(/(\d+)/)[0] //1662
Vladimirs
  • 8,232
  • 4
  • 43
  • 79
3

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)');
friedi
  • 4,350
  • 1
  • 13
  • 19