如何在 JavaScript 字符串替换中否定匹配正则表达式?
问题描述:
我试图用空白替换除数字以外的所有字符,但这不起作用:
I am trying to replace with a blank all characters except numbers, but this doesn't works:
s.replace(/(?!\d)/g, '')
谢谢!
答
使用否定字符类:
s.replace(/[^0-9]+/g, '')
或 s.replace(/[^\d]+/g, '')
或 s.replace(/\D+/g, '')