首页 > 解决方案 > 如何使用正则表达式匹配除加号之外的所有非数字字符?

问题描述

如何使用正则表达式匹配任何不是 0-9 和 + 的字符?我希望能够匹配除数字和加号以外的所有字符。

/\D+/ 匹配所有不是数字的字符,但我希望它对于数字和 + 为假

标签: javascriptregex

解决方案


使用[^0-9+]+模式,根据regex101 示例

[^0-9+]+

Match a single character not present in the list below [^0-9+]+

+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
+ matches the character + literally (case sensitive)

推荐阅读