为什么这是“如果”我期望在JS中正式表达的正则表达不正常
有人可以向我解释为什么在此代码中:
const textinputs = document.queryselectorall(selector);
textInputs.foreach(i => {
i.addeventListener('键',(e)=> {
if(e.key.match(/[^0-9]/ig)){
e.preventDefault();
}
});
});
条件“如果”确实可以正常工作。我希望当我输入任何数字时,E.PreventDefault()可以正常工作,但效果恰恰相反。我的意思是e.preventdefault()当我输入任何其他符号时工作时起作用
Could somebody explain to me why in this code:
const textInputs=document.querySelectorAll(selector);
textInputs.forEach(i=>{
i.addEventListener('keypress',(e)=>{
if(e.key.match(/[^0-9]/ig)){
e.preventDefault();
}
});
});
Condition "if" does work not as I expect. I expect the e.preventDefault() work when I enter any digits but it works exactly the opposite. I mean e.preventDefault() work when I enter any other symbol but a digits
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的正则态度中,
^
是否定的,这意味着将正则匹配的任何不属于数字的东西。尝试In your regex the
^
is a negation meaning the regex matches anything that is not digits. Try