├── README.md ├── aula1.js ├── aula10.js ├── aula2.js ├── aula3.js ├── aula4.js ├── aula5.js ├── aula6.js ├── aula7.js ├── aula8.js ├── aula9.js └── base.js /README.md: -------------------------------------------------------------------------------- 1 | # Seção de Expressões regulares (Curso de JS) 2 | 3 | Este é o repositório de expressões regulares do [Curso de JavaScript Moderno do básico ao avançado (2020)](https://www.udemy.com/course/curso-de-javascript-moderno-do-basico-ao-avancado/?referralCode=C6F07057B0999D3E36BA). 4 | -------------------------------------------------------------------------------- /aula1.js: -------------------------------------------------------------------------------- 1 | // g - global (encontra todas as ocorrências) 2 | // i - insensitive 3 | // (()()) grupos 4 | // | ou 5 | 6 | const { texto } = require('./base'); 7 | 8 | const regExp1 = /(maria|joão|luiz)(, hoje sua esposa)/i; 9 | const found = regExp1.exec(texto); 10 | 11 | if (found) { 12 | console.log(found[0]); 13 | console.log(found[1]); 14 | console.log(found[2]); 15 | } 16 | -------------------------------------------------------------------------------- /aula10.js: -------------------------------------------------------------------------------- 1 | // Encontra todas as palavras 2 | const palavrasRegEx = /([\wÀ-ú]+)/g 3 | 4 | // Não números 5 | const naoNumerosRegEx = /\D/g 6 | 7 | // Valida IP 8 | const ipRegExp = /((25[0-5]|2[0-4][0-9]|1\d{2}|[1-9]\d|\d)(\.)){3}(25[0-5]|2[0-4][0-9]|1\d{2}|[1-9]\d|\d)/g; 9 | 10 | // Valida CPF 11 | const cpfRegExp = /(?:\d{3}\.){2}\d{3}-\d{2}/g 12 | 13 | // Valida telefones 14 | const validaTelefone = /^(\(\d{2}\)\s*)?(9\s*)?(\d{4})-(\d{4})$/g 15 | 16 | // Validar senhas fortes 17 | const validaSenhasFortes = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%\]\)]).{8,}$/g 18 | 19 | // ################################## 20 | // # Validar e-mails (use a última) # 21 | // ################################## 22 | 23 | // [^\s]+@[^\s]+\.[^\s]+(\w+)* (Hiper Permissiva) 24 | 25 | // Essa expressão é bem permissiva, menos que a anterior, 26 | // Mas ainda detecta bastante e-mails inválidos 27 | // [^\s\.]+\.?[^\s\.]+@[^\s]+\.[^\s]+\w+ 28 | 29 | // Em meus testes a regexp que obteve melhor resultado 30 | // (acertando mais válidos e inválidos) 31 | // Foi a da linha a seguir: 32 | // /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 33 | const validaEmail = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 34 | -------------------------------------------------------------------------------- /aula2.js: -------------------------------------------------------------------------------- 1 | const { texto } = require('./base'); 2 | const regExp1 = /João|Maria/gi; 3 | 4 | // (...( () ) )(...)(...) $1 $2 $3 $4 $5 5 | 6 | // console.log(texto); 7 | // console.log(texto.match(regExp1)); 8 | // console.log(texto.replace(/(João|Maria)/gi, '$1')); 9 | // console.log(texto.replace(/(João|Maria)/gi, function(input) { 10 | // return ' ############## ' + input.toUpperCase() + ' ############## '; 11 | // })); 12 | -------------------------------------------------------------------------------- /aula3.js: -------------------------------------------------------------------------------- 1 | const { texto, arquivos } = require('./base'); 2 | 3 | // * (opcionais) 0 ou n {0,} 4 | // + (obrigatório) 1 ou n {1,} 5 | // ? (opcionais) 0 ou 1 {0,1} 6 | // \ Caractere de escape 7 | // {n,m} mínimo e máximo 8 | // {10,} no mínimo 10 9 | // {,10} de 0 a 10 10 | // {5,10} de 5 a 10 11 | // {10} 12 | 13 | // console.log(texto); 14 | // const regExp1 = /Jo+ão+/gi 15 | // console.log(texto.match(regExp1)); 16 | 17 | const regExp2 = /\.((jp|JP)(e|E)?(g|G))/g 18 | 19 | for (const arquivo of arquivos) { 20 | const valido = arquivo.match(regExp2); 21 | 22 | // if(!valido) continue; 23 | 24 | console.log(arquivo, valido); 25 | } -------------------------------------------------------------------------------- /aula4.js: -------------------------------------------------------------------------------- 1 | const { html } = require('./base'); 2 | 3 | console.log(html); 4 | console.log(html.match(/<.+>.+<\/.+>/g)); // greedy 5 | console.log(html.match(/<.+?>.+?<\/.+?>/g)); // non-greedy -------------------------------------------------------------------------------- /aula5.js: -------------------------------------------------------------------------------- 1 | const { alfabeto } = require('./base'); 2 | 3 | // [abc] -> Conjunto [^] -> Negação 4 | // [0-9] 5 | // [min-maxQUALQUERCOISA] 6 | // [^min-maxQUALQUERCOISA] -> Tudo menos isso 7 | console.log(alfabeto); 8 | console.log(alfabeto.match(/[0-9]+/g)); 9 | console.log(alfabeto.match(/[a-z]+/g)); 10 | console.log(alfabeto.match(/[A-Z]+/g)); 11 | console.log(alfabeto.match(/[^a-zA-Z0-9]+/g)); // Negação 12 | console.log(alfabeto.match(/[\u00A0-\u00BA]+/g)); // Unicode 13 | console.log(alfabeto.match(/\w+/g)); 14 | console.log(alfabeto.match(/\W/g)); 15 | console.log(alfabeto.match(/\d+/g)); 16 | console.log(alfabeto.match(/\D+/g)); 17 | console.log(alfabeto.match(/\s+/g)); 18 | console.log(alfabeto.match(/\S+/g)); -------------------------------------------------------------------------------- /aula6.js: -------------------------------------------------------------------------------- 1 | const { ips, cpfs } = require('./base'); 2 | 3 | // ^ -> Começa com 4 | // $ -> Termina com 5 | 6 | // console.log(cpfs); 7 | // console.log(cpfs.match(/[0-9]{3}\.[0-9]{3}\.[0-9]{3}\-[0-9]{2}/g)); 8 | // console.log(cpfs.match(/\d{3}\.\d{3}\.\d{3}\-\d{2}/g)); 9 | console.log(cpfs.match(/(\d{3}\.){2}\d{3}\-\d{2}/g)); 10 | 11 | // 250 - 255 25[0-5] 12 | // 200 - 249 2[0-4][0-9] 13 | // 100 - 199 1\d{2} 14 | // 10 - 99 [1-9]\d 15 | // 0 - 9 \d 16 | 17 | const ipRegExp = /((25[0-5]|2[0-4][0-9]|1\d{2}|[1-9]\d|\d)(\.)){3}(25[0-5]|2[0-4][0-9]|1\d{2}|[1-9]\d|\d)/g; 18 | 19 | // for (let i = 0; i <= 300; i++) { 20 | // const ip = `${i}.${i}.${i}.${i}`; 21 | // console.log(ip, ip.match(ipRegExp)); 22 | // } 23 | 24 | console.log(ips); 25 | console.log(ips.match(ipRegExp)); 26 | -------------------------------------------------------------------------------- /aula7.js: -------------------------------------------------------------------------------- 1 | const { cpfs, cpfs2 } = require('./base'); 2 | 3 | // ^ -> Começa com 4 | // $ -> Termina com 5 | // [^] -> Negação 6 | // m - multiline 7 | 8 | const cpf = ' 254.224.877-45'; 9 | 10 | const cpfRegExp = /^(\d{3}\.){2}\d{3}\-\d{2}$/gm; 11 | console.log(cpfs2); 12 | console.log(cpfs2.match(cpfRegExp)); 13 | -------------------------------------------------------------------------------- /aula8.js: -------------------------------------------------------------------------------- 1 | const { html2 } = require('./base'); 2 | 3 | // $1 $2 $3 <- Retrovisores \1 4 | 5 | // p Olá mundo 6 | //

Olá mundo

7 | 8 | // console.log(html2); 9 | // console.log(html2.match(/<(\w+)[\s\S]*?>([\s\S]*?)<\/\1>/g)); 10 | console.log(html2.replace(/(<(\w+)(?:[\s\S]*?)>)([\s\S]*?)(<\/\2>)/g, '$1 HAHA $3 HAHA $4')); 11 | -------------------------------------------------------------------------------- /aula9.js: -------------------------------------------------------------------------------- 1 | const { lookahead } = require('./base'); 2 | 3 | console.log(lookahead); 4 | // console.log(lookahead.match(/.+[^in]active$/gim)) 5 | 6 | // Positive lookahead (frases que tem active) 7 | // console.log(lookahead.match(/.+(?=[^in]active)/gim)) 8 | 9 | // Positive lookahead (frases que tem inactive) 10 | // console.log(lookahead.match(/.+(?=\s+inactive)/gim)) 11 | 12 | // Negative lookahead (frases que NÃO tem active) 13 | // console.log(lookahead.match(/^(?!.+[^in]active).+$/gim)) 14 | 15 | // Negative lookahead (frases que NÃO tem active) 16 | // console.log(lookahead.match(/^(?!.+inactive).+$/gim)) 17 | 18 | // Positive lookbehind (Frases que começam com ONLINE) 19 | // console.log(lookahead.match(/(?<=ONLINE\s+)\S+.*/gim)) 20 | 21 | // Negative lookbehind (Frases que NÃO começam com ONLINE) 22 | // console.log(lookahead.match(/^.+(?Olá mundo

Olá de novo

Sou a div
'; 61 | const html2 = `

64 | Olá mundo 65 |

Olá mundo

Sou a div
`; 66 | 67 | const lookahead = ` 68 | ONLINE 192.168.0.1 ABCDEF inactive 69 | OFFLINE 192.168.0.2 ABCDEF active 70 | ONLINE 192.168.0.3 ABCDEF active 71 | ONLINE 192.168.0.4 ABCDEF active 72 | OFFLINE 192.168.0.5 ABCDEF active 73 | OFFLINE 192.168.0.6 ABCDEF inactive 74 | `; 75 | 76 | module.exports = { 77 | texto, 78 | arquivos, 79 | html, 80 | html2, 81 | alfabeto, 82 | cpfs, 83 | ips, 84 | cpfs2, 85 | lookahead 86 | } --------------------------------------------------------------------------------