skip to Main Content

I need this function to return true if the string contains number, .dot or % sign.

Here are the. examples of values i need it to return true.

  1. 100
  2. 100.00
  3. 10%
isNumeric(value: any) {
  return /^-?d+$/.test(value);
}

2

Answers


  1. d|.|% would do?

    You can test the regex on sites such as regex101

    Login or Signup to reply.
  2. You could use the following regex:

    1. -? – optional minus sign
    2. d+ – integer part
    3. (.d+)? – optional decimal part
    4. %? – optional percent
    function isNumeric(value) {
      return /^-?d+(.d+)?%?$/.test(value);
    }
    
    '-10 100 100.00 10% 10.% 100. 10.5%'.split(' ').forEach(str => console.log(str, '=>', isNumeric(str)));
    .as-console-wrapper{max-height:100%!important}
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search