How to negative match regex in JavaScript string replace? [duplicate]

I am trying to replace with a blank all characters except numbers, but this doesn't works:

s.replace(/(?!\d)/g, '')

Thanks!

Answers:

Answer

Use negative character classes:

s.replace(/[^0-9]+/g, '')

or s.replace(/[^\d]+/g, '')

or s.replace(/\D+/g, '')

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.