我正在尝试编写一条正则替代字符串中不是数字或 .
的任何东西。
例如:
const string = 'I am a 1a.23.s12h31 dog'`
const result = string.replace(/[09.-]/g, '');
// result should be `1.23.1231`
任何人都可以看到我在这里做错了什么。
您可以将正格版更改为 [^0-9.]+
:
const result = string.replace(/[^0-9.]+/g, "");
另外,如果您不想要正则言论,请使用split
和filter
,则join
:
const result = string.split("").filter(s => isNaN(s) || s == ".").join("");