36 | export function currencyToRegex( |
37 | thousandSeparators = [' ', '.', ',', "'", '`'], |
38 | decimalNumbers = 2, |
39 | decimalSeparators = ['.', ','], |
40 | thousandGroupNumbers = 3 |
41 | ) { |
42 | const thousandSeparatorsRegex = arrayOfStringsToRegex( |
43 | thousandSeparators |
44 | ).join('|'); |
45 | const decimalSeparatorsRegex = arrayOfStringsToRegex(decimalSeparators).join( |
46 | '|' |
47 | ); |
48 | |
49 | const integerRegex = `(\\d{0,${thousandGroupNumbers - |
50 | 1}}(?:(?:${thousandSeparatorsRegex})?\\d{${thousandGroupNumbers}})*)`; |
51 | const decimalRegex = |
52 | decimalNumbers > 0 |
53 | ? `(?:(?:${decimalSeparatorsRegex})(\\d{0,${decimalNumbers}}))?` |
54 | : ''; |
55 | |
56 | return `^${integerRegex}${decimalRegex}$`; |
57 | } |