Combining Regex Patterns

Combining Regex Patterns
0.0 0


Hi guys, I have 2 regex, 1 to check a number is less than 15 significant figures, the other to check that same number is upto 2 decimal places(truncate)

1: /^-?(?=\d{1,15}(?:[.,]0+)?0*$|(?:(?=.{1,16}0*$)(?:\d+[.,]\d+)‌​)).+$/
2: /^-?(\d*\.?\d{0,2}).*/

I have almost 0 regex skill.

Question1: How do I combine the 2 regexes to do the work of both.

something like
/^-?(?=\d{1,15}(?:[.,]0+)?0*$|(?:(?=.{1,16}0*$)(?:\d+[.,]\d+)‌​)).+$ <&&ORAND>(\d*\.?\d{0,2}).*/

Then I tried and got something working,

const format = (number, regex) => String(number).replace(regex, '$1')


format(0.123456789012345, /^\d{1,13}(\.\d{1,2}|\d{0,2})$/)returns unformated number


format(0.123456789012345,/^-?(\d*\.?\d{0,2}).*/) number formatted to 2 deimal points as expected.

NOTE that both Regexes work on the site

Question2: Why does it not work in code/console/teminal

Thank you


You’re asking several questions.
To combine them you’d use |, unless you want to apply both in which case you’d just have to test the strings twice, once per regex.

The third regex test returns the same string as it doesn’t match the pattern. It’s expecting 0-2 numbers after the decimal point, not more.