Home > Mobile >  Regex to allow only number and the letter X
Regex to allow only number and the letter X

Time:09-28

Hi I need a regex to allow only numbers and the letter "x"... I'm trying something like this

target.value = target.value.replace(/[^x]\D/g, "")

But it deletes what's inputed already and it's still allowing special characters.

CodePudding user response:

You should put \d inside []. Otherwise you're removing any not-x followed by not-digit.

s = "abc123xyz";
console.log(s.replace(/[^x\d]/g, ''));

  • Related