Home > Enterprise >  Regex expression that requires a user to input a number as a %
Regex expression that requires a user to input a number as a %

Time:08-05

Can't figure out how to create a regex expression that requires the user to input a number as a %

CodePudding user response:

It's very simple, it looks like this:

const val1 = "10";
const val2 = "10%";

const checkPercentage = val => /\d %$/.test(val);

console.log(checkPercentage(val1));
console.log(checkPercentage(val2));

\d represents between 1 digit and

%$ means that the "%" sign must be at the end of the string.

;)

  • Related