正则表达式替换除数字和小数点以外的所有内容 [英] Regex to replace everything except numbers and a decimal point
本文介绍了正则表达式替换除数字和小数点以外的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本字段,只需要保留文本或小数。这是我正在使用的代码,用于替换除数字和小数点之外的所有内容。问题是,我无法找出能识别其他所有内容的正则表达式
I have a text field that needs to remain only text or decimal. Here is the code that I'm currently using to replace everything except numbers and a decimal point. Issue is, I can't figure out a regex that will identify everything else
document.getElementById(target).value = newVal.replace(/\D[^\.]/g, "");
\ D工作正常,但我试过(?! 。),(?!\。),[^。],[^ \。]
依此类推......
The \D works fine, but I've tried (?!.), (?!\.), [^.], [^\.]
and so on...
有关正则表达式的任何建议,除了数字或小数外都能正确识别吗?
Any suggestions for a regular expression that identifies positively with anything except a number or a decimal?
感谢您的帮助
推荐答案
使用此:
document.getElementById(target).value = newVal.replace(/[^0-9.]/g, "");
这篇关于正则表达式替换除数字和小数点以外的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文