Skip to content

Instantly share code, notes, and snippets.

@tosipaulo
Created September 22, 2020 14:32
Show Gist options
  • Save tosipaulo/59365515782a82b2981ca26f84e45a33 to your computer and use it in GitHub Desktop.
Save tosipaulo/59365515782a82b2981ca26f84e45a33 to your computer and use it in GitHub Desktop.
Mask
export class Mask {
static cpf_cnpj(value) {
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)(\.)(\d{2})(\d)(\.)(\d{2})(\d)(\-)(\d{3})/, '$1.$2$4.$5$7/$8$10')
.replace(/(\/\d{4})(\d{1,2})/, '$1-$2')
.replace(/(-\d{2})\d+?$/, '$1');
}
static cep(value) {
return value
.replace(/\D/g, '')
.replace(/(\d{5})(\d)/, '$1-$2')
.replace(/(-\d{3})\d+?$/, '$1');
}
static telefone(value) {
return value
.replace(/\D/g, '')
.replace(/(\d{2})(\d)/, '($1) $2')
.replace(/(\d{4})(\d)/, '$1-$2')
.replace(/(\d{4})-(\d)(\d{4})/, '$1$2-$3')
.replace(/(\d)(\d{4})(\d)/, '$1-$2')
.replace(/(-\d{4})\d+?$/, '$1');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment