1

If I have the following:

(8

Is there a way to get the number 8 out of it without splitting it? Using parseInt returns NaN. Is there an alternative to parseInt that ignores non-numbers?

David542
  • 104,438
  • 178
  • 489
  • 842
  • 1
    possible duplicate of [Javascript: strip out non-numeric characters from string](http://stackoverflow.com/questions/1862130/javascript-strip-out-non-numeric-characters-from-string) – Brendan Long May 29 '13 at 00:28

2 Answers2

7
parseInt(str.replace(/[^\d]/g, ''), 10)
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
3

You can use a quick regex to match that number, and just prepend + to cast to number:

var num =+ '(8'.match(/\d+/)
elclanrs
  • 92,861
  • 21
  • 134
  • 171