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
  • 101,766
  • 154
  • 423
  • 727
  • 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

6
parseInt(str.replace(/[^\d]/g, ''), 10)
SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933
3

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

var num =+ '(8'.match(/\d+/)
elclanrs
  • 89,567
  • 21
  • 132
  • 165