To match your values, you can use a non-exciting group with alternation with | and indicate what you want to match.
^(?:\.[1-9][0-9]?|\d{1,13}(?:\.\d{1,2})?|.\d{2})$
This will also match .01 , not .0
Explanation
^ Start line(?: Not an exciting group\.[1-9][0-9]? Matches a point not followed by 0 and then 0-9| Or\d{1,13} Match 1 - 13 digits(?: Not an exciting group\.\d{1,2} Match the dot and 1 or 2 digits
)? Close the group without capturing and make it optional| Or.\d{2} Match dot followed by 2 digits
) Close the group without capture$ End of line
const valid = [ "0", ".01", "0.5", "1.55", ".5", "1234567890123", "1234567890123.5", "1234567890123.00", ]; const invalid = [ ".", ".0", "1.234", "5.", "12345678901234", "12345678901234.56", ]; const rgx = /^(?:\.[1-9][0-9]?|\d{1,13}(?:\.\d{1,2})?|.\d{2})$/; console.log("Checking valid strings (should all be true):"); valid.forEach(str => console.log(rgx.test(str), str)); console.log("\nChecking invalid strings (should all be false):"); invalid.forEach(str => console.log(rgx.test(str), str));
To not match .01 , you can use:
^(?:\d{1,13}(?:\.\d{1,2})?|\.[1-9][0-9]?)$
const valid = [ "0", "0.5", "1.55", ".5", "1234567890123", "1234567890123.5", "1234567890123.00", ]; const invalid = [ ".", ".0", ".01", "1.234", "5.", "12345678901234", "12345678901234.56", ]; const rgx = /^(?:\d{1,13}(?:\.\d{1,2})?|\.[1-9][0-9]?)$/; console.log("Checking valid strings (should all be true):"); valid.forEach(str => console.log(rgx.test(str), str)); console.log("\nChecking invalid strings (should all be false):"); invalid.forEach(str => console.log(rgx.test(str), str));
source share