[英]Remove all non-numeric characters except one particular one
Forgive me, but regex is simply beyond me. 原谅我,但是正则表达式根本不在我身边。 I'm trying to use javascript to remove all non-numeric characters from a user input except the letter "m" in the first position.
我正在尝试使用javascript从用户输入中删除所有非数字字符,但首位字母“ m”除外。 I had this code which removes all non-numerics:
我有这段代码删除了所有非数字:
userInput.replace(/\D/g, '')
I'd like to modify this to not replace an "m" or "M" at the first position in the string, so 我想对此进行修改,以免在字符串的第一个位置替换“ m”或“ M”,因此
m490-333bA
would become 会成为
m490333
Any thoughts? 有什么想法吗?
您可以使用类似以下的表达式:
str.replace(/(?!^m)\D/ig, "");
您可以使用负数前瞻 :
userInput.replace(/(?!^M)\D/gi, '')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.