How to use 'regex currency' in JavaScript

Every line of 'regex currency' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
36export 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}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

61function getRegex (isDecimal: boolean): RegExp {
62 return new RegExp(
63 isDecimal
64 ? `^(0|[1-9]\\d*)(\\${KEYS.DECIMAL}\\d*)?$`
65 : '^(0|[1-9]\\d*)$'
66 );
67}

Related snippets