Last active
February 24, 2025 13:38
-
-
Save Santosl2/200eac0f4298a8a2192340dc97f8a373 to your computer and use it in GitHub Desktop.
Regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function cnpj(value: string) { | |
| if (!value) return value; | |
| return value | |
| .replace(/\D/g, "") | |
| .replace(/^(\d{2})(\d)/g, "$1.$2") | |
| .replace(/(\d{3})(\d)/, "$1.$2") | |
| .replace(/(\d{3})(\d)/, "$1/$2") | |
| .replace(/(\d{4})(\d)/, "$1-$2") | |
| .replace(/(-\d{2})(\d+?)$/, "$1"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function cpf(value: string) { | |
| return value | |
| .replace(/\D/g, "") | |
| .replace(/(\d{3})(\d)/, "$1.$2") | |
| .replace(/(\d{3})(\d)/, "$1.$2") | |
| .replace(/(\d{3})(\d{1,2})/, "$1-$2") | |
| .replace(/(-\d{2})\d+?$/, "$1"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Formatters { | |
| static hour(value: string) { | |
| if (!value) return value | |
| const formatter = value | |
| .replace(/\D/g, '') | |
| .replace(/(\d{2})(\d)/, '$1:$2') | |
| .replace(/(\d{2}):(\d{2}).+/, '$1:$2') | |
| return formatter | |
| } | |
| static cep(value: string) { | |
| if (!value) return value | |
| const formatter = value | |
| .replace(/\D/g, '') | |
| .replace(/^(\d{5})(\d)/g, '$1-$2') | |
| .replace(/-(\d{3})(\d{0,})/, '-$1') | |
| return formatter | |
| } | |
| static onlyNumbers(value: string) { | |
| return value.replace(/\D/g, '') | |
| } | |
| static money(value: number) { | |
| return new Intl.NumberFormat('pt-BR', { | |
| style: 'currency', | |
| currency: 'BRL', | |
| }).format(value) | |
| } | |
| } | |
| export { Formatters } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function phone(value: string) { | |
| return value | |
| .replace(/\D/g, "") | |
| .replace(/(\d{2})(\d)/, "($1) $2") | |
| .replace(/(\d{5})(\d{4})(\d)/, "$1-$2"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment