├── LICENSE └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Alexandre Servian 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Regex Cheat Sheet 2 | 3 | Para uma visão geral sobre o assunto, escrevi um [post](https://medium.com/@alexandreservian/regex-um-guia-pratico-para-express%C3%B5es-regulares-1ac5fa4dd39f) no medium que nos ajuda a entender melhor as regex. 4 | 5 | Todos os exemplos usam grupos do regex `()` para separar o simbolo do dado. Isto é feito para tornar a vida mais facil para o programador, quando for usar o método `replace`. 6 | Exemplo: 7 | 8 | ```javascript 9 | const text = "2019-26-09"; 10 | const regex = /(\d{4})-(\d{2})-(\d{2})/g; 11 | console.log(text.replace(regex, "$2/$3/$1")); 12 | // 26/09/2019 13 | ``` 14 | 15 | > Todos os dados foram gerados nos sites [4Devs](https://www.4devs.com.br/) e [Gerador Brasileiro](http://geradorbrasileiro.com/). 16 | 17 | 18 | ## Número de telefone 19 | 20 | ```javascript 21 | const text = ` 22 | - (77) 95684-9783 23 | - (71) 92184-0315 24 | - 905731497 25 | - 95088-2649 26 | - 70908447 27 | - 3421-8868 28 | - (6) 96237-7008 29 | - (68)90499-9922 30 | - (87) 997410611 31 | - (16)929439353 32 | `; 33 | const regex = /(\((\d{2})\)\s?)?(\d{4,5})[-]?(\d{4})/gm; 34 | console.log(text.match(regex)); 35 | /* 36 | [ 37 | '(77) 95684-9783', 38 | '(71) 92184-0315', 39 | '905731497', 40 | '95088-2649', 41 | '70908447', 42 | '3421-8868', 43 | '96237-7008', 44 | '(68)90499-9922', 45 | '(87) 997410611', 46 | '(16)929439353' 47 | ] 48 | */ 49 | ``` 50 | 51 | ## CEP 52 | 53 | ```javascript 54 | const text = ` 55 | - 58204-824 56 | - 69337-978 57 | - 69.938-863 58 | - 72874988 59 | `; 60 | const regex = /(\d{2}[.]?\d{3})[-]?(\d{3})/gm; 61 | console.log(text.match(regex)); 62 | // [ '58204-824', '69337-978', '69.938-863', '72874988' ] 63 | ``` 64 | 65 | ## CPF 66 | 67 | ```javascript 68 | const text = ` 69 | - 294.755.728-05 70 | - 337617902-60 71 | - 31568262353 72 | `; 73 | const regex = /(\d{3})[.]?(\d{3})[.]?(\d{3})[-]?(\d{2})/gm; 74 | console.log(text.match(regex)); 75 | // [ '294.755.728-05', '337617902-60', '31568262353' ] 76 | ``` 77 | 78 | ## CNPJ 79 | 80 | ```javascript 81 | const text = ` 82 | - 97.164.674/0001-63 83 | - 76293834/0001-02 84 | - 82142821/000127 85 | - 128913760001-12 86 | - 57783170000107 87 | `; 88 | const regex = /(\d{2})[.]?(\d{3})[.]?(\d{3})[/]?(\d{4})[-]?(\d{2})/gm; 89 | console.log(text.match(regex)); 90 | /* 91 | [ 92 | '97.164.674/0001-63', 93 | '76293834/0001-02', 94 | '82142821/000127', 95 | '128913760001-12', 96 | '57783170000107' 97 | ] 98 | */ 99 | ``` 100 | 101 | ## CPF e CNPJ ao mesmo tempo 102 | 103 | ```javascript 104 | const text = ` 105 | - 294.755.728-05 106 | - 337617902-60 107 | - 31568262353 108 | - 97.164.674/0001-63 109 | - 76293834/0001-02 110 | - 82142821/000127 111 | - 128913760001-12 112 | - 57783170000107 113 | `; 114 | const regex = /((\d{2})[.]?(\d{3})[.]?(\d{3})[/]?(\d{4})[-]?(\d{2}))|((\d{3})[.]?(\d{3})[.]?(\d{3})[-]?(\d{2}))/gm; 115 | console.log(text.match(regex)); 116 | /* 117 | [ 118 | '294.755.728-05', 119 | '337617902-60', 120 | '31568262353', 121 | '97.164.674/0001-63', 122 | '76293834/0001-02', 123 | '82142821/000127', 124 | '128913760001-12', 125 | '57783170000107' 126 | ] 127 | */ 128 | ``` 129 | 130 | ## Título de eleitor 131 | 132 | ```javascript 133 | const text = ` 134 | - 9165.1348.1830 135 | - 871560272755 136 | `; 137 | const regex = /(\d{4})[.]?(\d{4})[.]?(\d{4})/gm; 138 | console.log(text.match(regex)); 139 | // [ '9165.1348.1830', '871560272755' ] 140 | ``` 141 | 142 | ## PIS/PASEP 143 | 144 | ```javascript 145 | const text = ` 146 | - 038.54391.67-8 147 | - 8972716250-2 148 | - 74189540820 149 | `; 150 | const regex = /(\d{3})[.]?(\d{5})[.]?(\d{2})[-]?(\d)/gm; 151 | console.log(text.match(regex)); 152 | // [ '038.54391.67-8', '8972716250-2', '74189540820' ] 153 | ``` 154 | 155 | ## CNH 156 | 157 | ```javascript 158 | const text = ` 159 | - 151464680-66 160 | - 19533844800 161 | `; 162 | const regex = /(\d{9})[-]?(\d{2})/gm; 163 | console.log(text.match(regex)); 164 | // [ '151464680-66', '19533844800' ] 165 | ``` 166 | 167 | ## Placa de carros no padrão normal e o novo padrão Mercosul 168 | 169 | ```javascript 170 | const text = ` 171 | - RKN-4503 172 | - EDQ-5579 173 | - LOR0285 174 | - COM9A55 175 | - ANT1G05 176 | - MOD3L05 177 | `; 178 | const regex = /(([A-Z]{3})[-]?(\d{4}))|(([A-Z]{3})(\d{1})([A-Z]{1})(\d{2}))/gm; 179 | console.log(text.match(regex)); 180 | /* 181 | [ 182 | 'RKN-4503', 183 | 'EDQ-5579', 184 | 'LOR0285', 185 | 'COM9A55', 186 | 'ANT1G05', 187 | 'MOD3L05' 188 | ] 189 | */ 190 | ``` 191 | 192 | ## Renavam 193 | 194 | ```javascript 195 | const text = ` 196 | - 2406618318-6 197 | - 30693589258 198 | `; 199 | const regex = /(\d{10})[-]?(\d{1})/gm; 200 | console.log(text.match(regex)); 201 | // [ '2406618318-6', '30693589258' ] 202 | ``` 203 | 204 | ## Inscrição Estadual 205 | 206 | ```javascript 207 | const text = ` 208 | - AC - 01.618.974/339-10 209 | - AL - 2480456110 210 | - AP - 039519660 211 | - AM - 80.938.148-6 212 | - BA - 116884-93 213 | - CE - 53525140-8 214 | - DF - 07127383001-42 215 | - ES - 46249822-0 216 | - GO - 10.945.363-8 217 | - MA - 12749382-4 218 | - MT - 7901625213-0 219 | - MS - 28825429-5 220 | - MG - 087.075.583/0925 221 | - PA - 15-919530-6 222 | - PB - 41867640-2 223 | - PR - 042.31026-32 224 | - PE - 4956703-91 225 | - PI - 80788482-0 226 | - RJ - 89.048.76-1 227 | - RN - 20.210.143-6 228 | - RS - 993/7064762 229 | - RO - 2860516573540-8 230 | - RR - 24259146-1 231 | - SP - 365.456.289.105 232 | - SC - 975.869.620 233 | - SE - 22610269-6 234 | - TO - 9703391962-0 235 | `; 236 | const regex = /(?=\d)(?![\d/\-.]*[/\-.]{2})[\d/\-.]{1,16}\d/gm; 237 | console.log(text.match(regex)); 238 | /* 239 | [ 240 | '01.618.974/339-10', 241 | '2480456110', 242 | '039519660', 243 | '80.938.148-6', 244 | '116884-93', 245 | '53525140-8', 246 | '07127383001-42', 247 | '46249822-0', 248 | '10.945.363-8', 249 | '12749382-4', 250 | '7901625213-0', 251 | '28825429-5', 252 | '087.075.583/0925', 253 | '15-919530-6', 254 | '41867640-2', 255 | '042.31026-32', 256 | '4956703-91', 257 | '80788482-0', 258 | '89.048.76-1', 259 | '20.210.143-6', 260 | '993/7064762', 261 | '2860516573540-8', 262 | '24259146-1', 263 | '365.456.289.105', 264 | '975.869.620', 265 | '22610269-6', 266 | '9703391962-0' 267 | ] 268 | */ 269 | ``` 270 | 271 | ## Cartão Mastercard 272 | 273 | ```javascript 274 | const text = ` 275 | - 5125 8108 3239 3913 276 | - 5213-4033-9663-4675 277 | - 5392.9140.7548.2098 278 | - 5460805328094911 279 | - 5571073331809322 280 | `; 281 | const regex = /(?=[5][1-5])(\d{4})[\s-.]?(\d{4})[\s-.]?(\d{4})[\s-.]?(\d{4})/gm; 282 | console.log(text.match(regex)); 283 | /* 284 | [ 285 | '5125 8108 3239 3913', 286 | '5213-4033-9663-4675', 287 | '5392.9140.7548.2098', 288 | '5460805328094911', 289 | '5571073331809322' 290 | ] 291 | */ 292 | ``` 293 | 294 | ## Cartão Visa 295 | 296 | ```javascript 297 | const text = ` 298 | - 4929 8066 6172 1969 299 | - 4024.0071.9067.6451 300 | - 4916-1801-5471-1621 301 | - 4556201055723856 302 | `; 303 | const regex = /(?=[4])(\d{4})[\s-.]?(\d{4})[\s-.]?(\d{4})[\s-.]?(\d{4})/gm; 304 | console.log(text.match(regex)); 305 | /* 306 | [ 307 | '4929 8066 6172 1969', 308 | '4024.0071.9067.6451', 309 | '4916-1801-5471-1621', 310 | '4556201055723856' 311 | ] 312 | */ 313 | ``` 314 | 315 | ## CVV dos cartões Mastercard ou Visa 316 | 317 | ```javascript 318 | const text = ` 319 | - 894 320 | - 815 321 | `; 322 | const regex = /(\d{3})/gm; 323 | console.log(text.match(regex)); 324 | // [ '894', '815' ] 325 | ``` 326 | 327 | ## Validade dos cartões Mastercard ou Visa 328 | 329 | ```javascript 330 | const text = ` 331 | - 06/20 332 | - 03-21 333 | - 0425 334 | `; 335 | const regex = /(?=(0[1-9])|(1[0-2]))([0-1][0-9])[/-]?(\d\d)/gm; 336 | console.log(text.match(regex)); 337 | // [ '06/20', '03-21', '0425' ] 338 | ``` 339 | 340 | ## Validação de senha forte 341 | 342 | Vamos criar um exemplo de validação de senha forte. Valida se tem: 343 | - Pelo menos uma letra maiúscula; 344 | - Pelo menos um número; 345 | - Pelo menos um simbolo `(!@#$%&*()-+.,;?{[}]^><:)`; 346 | - Que tenha entre 6 a 12 caracteres; 347 | - Aceitando somente letras de `a - z` maiúscula e minúscula, números e os simbolos `(!@#$%&*()-+.,;?{[}]^><:)` 348 | 349 | > Neste exemplo, vamos usar o método `test` do objeto `RegExp`, para testar se a string contem ou não nossa regra. 350 | 351 | ```javascript 352 | const senha1 = '#1Wl4i'; 353 | const senha2 = 'mm7i%KX^+'; 354 | const senha3 = 'Q3tYR+Y1dL:P'; 355 | const senha4 = 'y-fw6&q'; 356 | const senha5 = 'kA{Lbo'; 357 | const senha6 = 'uuksEy6'; 358 | const senha7 = '(8gp30d0@%;Ms}0'; 359 | const senha8 = 'U:oTKrçãé'; 360 | const regex = /^(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%&*()+\-.,;?\^.,;?><:{}\[\]])[\w!@#$%&*()+\-.,;?\^.,;?><:{}\[\]]{6,12}$/; 361 | console.log(regex.test(senha1)); //true 362 | console.log(regex.test(senha2)); //true 363 | console.log(regex.test(senha3)); //true 364 | console.log(regex.test(senha4)); //false (não contem uma letra maiúscula) 365 | console.log(regex.test(senha5)); //false (não contem pelo menos um número) 366 | console.log(regex.test(senha6)); //false (não contem pelo menos um simbolo) 367 | console.log(regex.test(senha7)); //false (contem mais de 12 caracteres) 368 | console.log(regex.test(senha8)); //false (contem caracteres diferentes de letras de a-z, números ou simbolo) 369 | ``` 370 | 371 | ## Validação de email 372 | 373 | ```javascript 374 | const email1 = 'kpastornilson.i@worthwre.com'; 375 | const email2 = '7ahmed.medo.35178@learnwithvideo.org'; 376 | const email3 = 'phoussam@shift-coin.com'; 377 | const email4 = 'fsimo.test.12q@6686088-.com'; 378 | const email5 = 'llokomcap@bedfadsfaidsok.live.f'; 379 | const regex = /^(\S+)@((?:(?:(?!-)[a-zA-Z0-9-]{1,62}[a-zA-Z0-9])\.)+[a-zA-Z0-9]{2,12})$/; 380 | console.log(regex.test(email1)); //true 381 | console.log(regex.test(email1)); //true 382 | console.log(regex.test(email3)); //true 383 | console.log(regex.test(email4)); //false 384 | console.log(regex.test(email5)); //false 385 | ``` 386 | 387 | > Todos emails foram gerados usando o site [generator.email](https://generator.email/) 388 | 389 | 390 | ## Validação de datas 391 | 392 | Nesta validação, valido as seguintes regras: 393 | - Se o ano é bissexto; 394 | - Se o mês pode ou não ter 31 dias; 395 | - Aceita os separadores `[\/\-.]`; 396 | - Dia e mês podem vir somente com um dígito; 397 | - Podemos ter a representação de ano, com 2 ou 4 dígitos; 398 | 399 | > Eu subi tambem varios testes no site [regex101](https://regex101.com/r/f9avRz/12). 400 | 401 | ```javascript 402 | const data1 = '29/02/2002'; 403 | const data2 = '29/02/2000'; 404 | const data3 = '29/02/1998'; 405 | const data4 = '29/02/52'; 406 | const data5 = '31/7/1998'; 407 | const data6 = '29/03/1968'; 408 | const data7 = '31/09/1998'; 409 | const data8 = '05/4/2000'; 410 | const regex = /^((?:(?=29[\/\-.]0?2[\/\-.](?:[1-9]\d)?(?:[02468][048]|[13579][26])(?!\d))29)|(?:(?=31[\/\-.](?!11)0?[13578]|1[02])31)|(?:(?=\d?\d[\/\-.]\d?\d[\/\-.])(?!29[\/\-.]0?2)(?!31)(?:[12][0-9]|30|0?[1-9])))[\/\-.](0?[1-9]|1[0-2])[\/\-.]((?:[1-9]\d)?\d{2})$/; 411 | console.log(regex.test(data1)); //false 412 | console.log(regex.test(data2)); //true 413 | console.log(regex.test(data3)); //false 414 | console.log(regex.test(data4)); //true 415 | console.log(regex.test(data5)); //true 416 | console.log(regex.test(data6)); //true 417 | console.log(regex.test(data7)); //false 418 | console.log(regex.test(data8)); //true 419 | ``` 420 | 421 | 422 | ## Links 423 | 424 | [4Devs](https://www.4devs.com.br/) 425 | [Gerador Brasileiro](http://geradorbrasileiro.com/) 426 | [O que significa cada número do cartão de crédito](https://www.tecmundo.com.br/cartao-de-credito/43322-o-que-significa-cada-numero-do-cartao-de-credito-ilustracao-.htm) --------------------------------------------------------------------------------