Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 663
Edu Lomeli Avatar asked Dec 15 '22 21:12

Edu Lomeli


1 Answers

Use negative character classes:

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

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

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

like image 81
Lucas Trzesniewski Avatar answered Dec 17 '22 11:12

Lucas Trzesniewski