admin管理员组

文章数量:1422453

I know that dozens of similar questions was asked before, but cannot find solution which fits my needs.

I need a regular expression as a pattern for validation purposes, which is a range between 0.025 to 99.999 but should also match integers and any possible decimal forms up to 3 decimals:

1 - matched

1.0 - matched

1.000 - matched

0.025 - matched

0.03 - matched

0.01 - not matched

0.024 - not matched

So far my regex looks like this:

^(?!0*(\.0+)?$)([1-9]?[0-9]\.[0-9]{0,3}|[1-9]?[0-9])?$

It actually matches all between 0.001 and 99.999, because managed how to exclude 0 with decimal forms, but don't know how in the easiest way exclude 0.001 to 0.024.

I know that dozens of similar questions was asked before, but cannot find solution which fits my needs.

I need a regular expression as a pattern for validation purposes, which is a range between 0.025 to 99.999 but should also match integers and any possible decimal forms up to 3 decimals:

1 - matched

1.0 - matched

1.000 - matched

0.025 - matched

0.03 - matched

0.01 - not matched

0.024 - not matched

So far my regex looks like this:

^(?!0*(\.0+)?$)([1-9]?[0-9]\.[0-9]{0,3}|[1-9]?[0-9])?$

It actually matches all between 0.001 and 99.999, because managed how to exclude 0 with decimal forms, but don't know how in the easiest way exclude 0.001 to 0.024.

Share Improve this question edited Sep 25, 2018 at 15:09 mpro asked Sep 25, 2018 at 14:38 mprompro 15k5 gold badges33 silver badges44 bronze badges 2
  • 2 Is it required to use a regex? Because it's quite a lot simpler to not use one and just check if the number is within the range you want. – VLAZ Commented Sep 25, 2018 at 14:42
  • @vlaz I need to provide regex patter for validation purposes – mpro Commented Sep 25, 2018 at 15:05
Add a ment  | 

2 Answers 2

Reset to default 7

in javascript you can just convert the string to number and use logic like this

const str = '1.0'
const test = +str;

if(test >= 0.025 && test <= 99.999) {
  console.log('valid');
}

IMO this will be simpler and easier to read

EDIT:

as mentioned in ments this will pass for strings like 3e-2 which is valid syntax in javascript to define numbers, so you might have to handle that case based on your use case.

To do this with a regular exression, you need something a bit plex:

^(?:0\.02[5-9]|0\.0[3-9]\d?|0\.[1-9]\d{0,2}|[1-9][0-9]?(?:\.\d{1,3})?)$

Here it is in Regex101

Here is how this works in JS

const regex = /^(?:0\.02[5-9]|0\.0[3-9]\d?|0\.[1-9]\d{0,2}|[1-9][0-9]?(?:\.\d{1,3})?)$/;

const testValues = [ "0", "1", "99.999", "0.025", "0.024", "0.01" ];

testValues.forEach(value => console.log(value, regex.test(value)));

Explanation for the expression: it's a bunch of ORs that cover the value range. Although you can press it more, I've tried to keep it logical and readable:

  • 0\.02[5-9] - values from 0.025 to 0.029
  • 0\.0[3-9]\d? - values from 0.03 to 0.099. The last digit is optional.
  • 0\.[1-9]\d{0,2} - values from 0.1 to 0.999. The last two digits are optional. The fractional part in each case up to now is mandatory, so 0 is not valid.
  • [1-9][0-9]?(?:\.\d{1,3}) - values from 1 to 99.999. Again, the decimal point and fractional part are set as optional. If present, you can have 1-3 of them.
  • the entire regex is wrapped in a non-capturing group and then nested between ^ and $ anchors to make sure that the ENTIRE string matches.

Notable exclusions with this regular expression:

  • exponential form/scientific notation. For example: 3e1 or 3e-1
  • numbers that lead with zero. For example: 01, 02.345
  • numbers that start with a sign. For example: +10
  • values that start from the decimal part. For example: 0.2, 0.123
  • values that end in a dot. For example 1., 2.
  • strings that contain whitespace. For example: " 1" will fail to match. The easiest way to solve this is by ensuring the values are trimmed before being tested.

本文标签: javascriptRegular expression of decimal range 0025 to 99999Stack Overflow