Regex not working in Angular Validators.pattern() while working in online regex testers
Use the following fix:
Validators.pattern(/^\+?(?:[1-9]\d*(?:\.\d{1,2})?|0\.(?:[1-9]\d?|\d[1-9]))$/)
The regex demo is here.
Make sure:
- You define the regex as a regex literal (not a string,
/.../
should not be wrapped with any quotes - If you use a string pattern, make sure you double escape the backslashes and then, you do not need to use
^
and$
at both ends as they will be added automatically.
The code above is equal to
Validators.pattern("\\+?(?:[1-9]\\d*(?:\\.\\d{1,2})?|0\\.(?:[1-9]\\d?|\\d[1-9]))")