I am working with a regex named 'pattern' that is intended to allow only numbers as input. However, I'm noticing that both pattern.test("a")
and pattern.test("1")
are unexpectedly returning true. Can anyone explain why this is happening?
const pattern = /^[a-zA-Z0-9]*$/;
if (!pattern.test(event.target.value)) {
event.target.value = event.target.value.replace(/[^a-zA-Z0-9]/g, "");
}