javascript - parseInt alternative with characters in string - Stack Overflow

If I have the following:(8Is there a way to get the number 8 out of it without splitting it? Using pars

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?

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?

Share Improve this question asked May 29, 2013 at 0:26 David542David542 111k206 gold badges571 silver badges1k bronze badges 1
  • 1 possible duplicate of Javascript: strip out non-numeric characters from string – Brendan Long Commented May 29, 2013 at 0:28
Add a ment  | 

2 Answers 2

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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745264885a4619395.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信