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

帅比萌擦擦* 提交于 2019-12-11 03:15:04

问题


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

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

Thanks!


回答1:


Use negative character classes:

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

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

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



来源:https://stackoverflow.com/questions/24419609/how-to-negative-match-regex-in-javascript-string-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!