Regex.ao
regex.ao é uma coleção de expressões regulares mais comuns e que podemos querer implementar durante o desenvolvimento das nossas aplicações. Agora com esse repositório é só procurar e colar no seu projeto.
Você pode executar os test's dessas regex's clonando o projeto e instalando as depedências, e depois rodar simplesmente: yarn test
Índice:
- Remover acentos em strings
- Remover hífens em strings
- Remover vogais em strings
- Remover números em strings
- Permitir somente números
- Verificar se é uma URL
- Validação de email
Remover acentos em Strings
function removeAccents(value) {
return value.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
}
//input: Maçãs
//output: Macas
Remover hífens em Strings
const hyphensRemoved = (value) => {
return value.replace(/-/g, ' ');
}
//input: 1-2-3-2
//output: 1 2 3 2
Remover todas vogais em Strings
const removeAllVowels = (value) => {
return value.replace(/[aeiou]/gi, '')
}
//input: daily tips
//output: dly tps
Remover números em Strings
function removeNumber(s) {
return s.replace(/[0-9]/g, "");
}
//input: george21
//output: george
Permitir somente Números
function allowNumber(value) {
return /^[0-9]*$/.test(value);
}
//input: george21 //input: 230
//output: false //output: true
URL
Verificar se é uma function isURL(value) {
const regex =
/^(?:https?):\/\/(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/;
return regex.test(value);
}
//input: www.martins.com //input: https:github.com/martygo
//output: false //output: true
Verificar se um email é válido
function emailValidator(email) {
const regex =
/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/;
return regex.test(email);
}
//input: me#gmail.com // false
//output: vodu@gmail.com // true
Alguma sugestão?
Abra uma issue