David542
David542

Reputation: 110380

parseInt alternative with characters in string

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?

Upvotes: 1

Views: 1595

Answers (2)

elclanrs
elclanrs

Reputation: 94121

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

var num =+ '(8'.match(/\d+/)

Upvotes: 3

SLaks
SLaks

Reputation: 887807

parseInt(str.replace(/[^\d]/g, ''), 10)

Upvotes: 7

Related Questions