3 examples of 'regex for decimal number' in JavaScript

Every line of 'regex for decimal number' 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
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}
68function removeDecimal(num) {
69 const re = new RegExp('^-?\\d+');
70 return num.toString().match(re)[0];
71}
5public static validate(decimals: number): (control: FormControl) => { pattern: string } | null {
6 const regExpString: string = `^[0-9]+((\\.|,){1}[0-9]\\d{0,${decimals - 1}})?$`
7 const regExp: RegExp = new RegExp(regExpString)
8
9 return (control: FormControl): { pattern: string } | null => {
10 const stringAmount: string = new BigNumber(control.value).toFixed()
11 if (stringAmount.match(regExp) === null) {
12 return { pattern: 'Pattern does not match.' }
13 }
14
15 return null
16 }
17}

Related snippets