最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - parseInt alternative with characters in string - Stack Overflow

programmeradmin1浏览0评论

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+/)
发布评论

评论列表(0)

  1. 暂无评论