以替换字符串中没有数字或周期的任何内容



我正在尝试编写一条正则替代字符串中不是数字或 .的任何东西。

例如:

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, "");

另外,如果您不想要正则言论,请使用splitfilter,则join

const result = string.split("").filter(s => isNaN(s) || s == ".").join("");

相关内容

最新更新