-5

I'm looking for a regular expression in JavaScript that tests whether a string is a number with one or two digits before the decimal point and optionally a maximum of five digits after decimal point.

Examples of correct values:

  • 12.345
  • 12.12
  • 1.3
  • 1.12345
  • 12

What would be the correct regex for this?

John Slegers
  • 45,213
  • 22
  • 199
  • 169
BharathKS
  • 51
  • 2

2 Answers2

2

you can try that:

^\d{1,2}(\.\d{1,5})?$

Explanation:

  1. ^ start of a string
  2. \d{1,2} 1 to 2 digit number
  3. ( opening capture group
  4. \. dot
  5. \d{1,5} number 1 to 5 digit
  6. ) closing capture group
  7. ? makes the entire capture group optional

$ end of string

Demo

const regex = /^\d{1,2}(\.\d{1,5})?$/gm;
const str = `12.121`;

console.log(regex.test(`12.121`));
console.log(regex.test(`1`));
console.log(regex.test(`1.1`));
console.log(regex.test(`12.123`));
console.log(regex.test(`1.123`));
console.log(regex.test(`1.1234567`));
Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43
  • It works, but you should explain the regex, rather than just post it. Also, it would be better to include a snippet in the answer, as opposed to an off-site link. That said, you put more effort into it than the OP, so maybe it should just be closed :p – Reinstate Monica Cellio Feb 12 '18 at 09:06
  • I am trying to use this in angular4 validator pattern and its not working there.So can u pls suggest one which can be used in Validator patterns or the right way to use Validator patterns – BharathKS Feb 12 '18 at 09:11
  • @bharathkells Read [ask] and then post a question, with your code, that fits the criteria for asking good questions. Don't try and hijack someone who genuinely tried to help you, by shoving new information in their face and expecting them to do your job for you. Read and then ask again ;) – Reinstate Monica Cellio Feb 12 '18 at 09:15
  • really sorry,Next time i ll for sure read How to Ask and post a question .Anyways thank you for the explaination. – BharathKS Feb 12 '18 at 09:38
  • @Rizwan does this work for 12. ? – BharathKS Feb 12 '18 at 09:41
  • no. 12dot is invalid or do you want to mark that as valid ? – Mustofa Rizwan Feb 12 '18 at 11:01
0

Regex

/^\d{1,2}(\.\d{1,5})?$/

Demo

var regexp = /^\d{1,2}(\.\d{1,5})?$/;

console.log("'10.5' returns " + regexp.test('10.5'));
console.log("'100.5' returns " + regexp.test('100.5'));
console.log("'82.744' returns " + regexp.test('82.744'));
console.log("'13.' returns " + regexp.test('13.'));
console.log("'.744' returns " + regexp.test('.744'));
console.log("'.74400' returns " + regexp.test('.74400'));
console.log("'5.74400' returns " + regexp.test('5.74400'));

Explanation

  1. / / : the beginning and end of the expression
  2. ^ : whatever follows should be at the beginning of the string you're testing
  3. \d{1,2} : there should be one or two digits here
  4. ( )? : this part is optional
  5. \. : here goes a dot
  6. \d{1,5} : there should be between one and five digits here
  7. $ : whatever precedes this should be at the end of the string you're testing

Tip

You can use regexr.com or regex101.com for testing regular expressions directly in the browser!

John Slegers
  • 45,213
  • 22
  • 199
  • 169