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

5
  • 1
    Is the string always in this format? If so, you can use regex.. Commented Nov 20, 2014 at 16:02
  • You could use a regexp ;) Commented Nov 20, 2014 at 16:03
  • Possible dublicate - stackoverflow.com/questions/1623221/… Commented Nov 20, 2014 at 16:03
  • possible duplicate of strip non-numeric characters from string Commented Nov 20, 2014 at 16:04
  • Yup string will be always in this format Commented Nov 20, 2014 at 16:04

2 Answers 2

4

You can use regex for that like:

"Bloomfield College (1662) (COL)".match(/(\d+)/)[0] //1662
Sign up to request clarification or add additional context in comments.

Comments

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)');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.