0
  1. /^$| (^100([.]0{1,2})?)$|(^\\d{1,2}([.]\\d{1,2})?)$/

    The regular expression above passes 00.00 to 99.99 but not 100 or 100.00

  2. /^$|(^100([.]0{1,2})?)$|(^\\d{1,2}([.]\\d{1,2})?)$/

    Above regular expression passes 00.00 to 99.99 with 100 or 100.00

But only one difference between them is a space '' after | operator.

Thanks in advance.

Samuel Hulla
  • 5,279
  • 6
  • 25
  • 51
K Dey
  • 19
  • 3
  • `^` asserts position at start of a line. It can not assert potion because you have space in front of it. [demo](https://regex101.com/r/0WGo0a/1) – Srdjan M. Aug 24 '18 at 16:41

3 Answers3

0

You have an extra space:

/^$| (^100([.]0{1,2})?)$|(^\\d{1,2}([.]\\d{1,2})?)$/    
# __^ here

Remove it.

The second alternative (^100([.]0{1,2})?)$ could not match anything because it is not possible to have a space before the beginning of line ^

Toto
  • 83,193
  • 59
  • 77
  • 109
0

The space makes a big difference. The first one will only match 100 or 100.00 if there's a space before it, since it's a required character in that alternative.

Barmar
  • 596,455
  • 48
  • 393
  • 495
0

Adding a space is going to invalid the second alternative :

(^100([.]0{1,2})?)

because it says it must start with a space character when it also specify a ^.

The regex with the space is equivalent to the regex without the second alternative:

^$|(^\d{1,2}([.]\d{1,2})?)$

Let's see the following snippet.

const reg1 = /^$| (^100([.]0{1,2})?)$|(^\d{1,2}([.]\d{1,2})?)$/;
const reg2 = /^$|(^100([.]0{1,2})?)$|(^\d{1,2}([.]\d{1,2})?)$/;

const str1 = '100';
const str2 = '90';

console.log(reg1.test(str1));
console.log(reg1.test(str2));

console.log('----------------------------');

console.log(reg2.test(str1));
console.log(reg2.test(str2));

const reg3 = /^$|(^\d{1,2}([.]\d{1,2})?)$/;

console.log('----------------------------');

console.log(reg3.test(str1));
console.log(reg3.test(str2));

For the help you can go to https://regex101.com

enter image description here

Orelsanpls
  • 18,380
  • 4
  • 31
  • 54