Expressão Regular
Sintaxe
Names | Regexp |
---|---|
FLAGS | g, i, m, u, y |
CHARACTER CLASSES | . , \d, \D, \w, \W, \s, \S, \ |
CHARACTER SET | [], [^] |
BOUNDARIES | ^, $, \b, \B |
ALTERNATION | | |
GROUPING AND BACK REFERENCES | (), \n, (?:x) |
QUANTIFIERS | *, +, ?, {n}, {n,}, {n, m} (Greedy), x*?, x+?, x??, x{n}?, x{n,}?, x{n,m}? (Non-Greedy) |
ASSERTIONS | x(?=y), x(?!y) |
Padrões
Byte
/[01]{8}/
01010101
11110000
11110002
01010101
11110000
11110002
const pattern = /[01]{8}/;
console.log(pattern.test('01010101')); //=> true
console.log(pattern.test('11110000')); //=> true
console.log(pattern.test('11110002')); //=> false
const pattern = /[01]{8}/;
console.log(pattern.test('01010101')); //=> true
console.log(pattern.test('11110000')); //=> true
console.log(pattern.test('11110002')); //=> false
CPF
/^(\d{11}|\d{3}\.\d{3}\.\d{3}-\d{2})$/
11122233344
111.222.333-44
111x222.333-44
1112223334
111222333440
11122233344X
11122233344
111.222.333-44
111x222.333-44
1112223334
111222333440
11122233344X
const pattern = /^(\d{11}|\d{3}\.\d{3}\.\d{3}-\d{2})$/;
console.log(pattern.test('11122233344')); //=> true
console.log(pattern.test('111.222.333-44')); //=> true
console.log(pattern.test('111x222.333-44')); //=> false
console.log(pattern.test('1112223334')); //=> false
console.log(pattern.test('111222333440')); //=> false
console.log(pattern.test('11122233344X')); //=> false
const pattern = /^(\d{11}|\d{3}\.\d{3}\.\d{3}-\d{2})$/;
console.log(pattern.test('11122233344')); //=> true
console.log(pattern.test('111.222.333-44')); //=> true
console.log(pattern.test('111x222.333-44')); //=> false
console.log(pattern.test('1112223334')); //=> false
console.log(pattern.test('111222333440')); //=> false
console.log(pattern.test('11122233344X')); //=> false
CEP
/^(\d{8}|\d{2}\.?\d{3}-\d{3})$/
01001000
01001-000
01.001-000
a01001000
01-001.000
01.001000
01001000
01001-000
01.001-000
a01001000
01-001.000
01.001000
const pattern = /^(\d{8}|\d{2}\.?\d{3}-\d{3})$/;
console.log(pattern.test('01001000')); //=> true
console.log(pattern.test('01001-000')); //=> true
console.log(pattern.test('01.001-000')); //=> true
console.log(pattern.test('a01001000')); //=> false
console.log(pattern.test('01-001.000')); //=> false
console.log(pattern.test('01.001000')); //=> false
const pattern = /^(\d{8}|\d{2}\.?\d{3}-\d{3})$/;
console.log(pattern.test('01001000')); //=> true
console.log(pattern.test('01001-000')); //=> true
console.log(pattern.test('01.001-000')); //=> true
console.log(pattern.test('a01001000')); //=> false
console.log(pattern.test('01-001.000')); //=> false
console.log(pattern.test('01.001000')); //=> false
Desafio
Qual é a expressão da hora no padrão 24h (hh:mm)?
00:00 - válidos
00:01 - válidos
20:00 - válidos
23:59 - válidos
23:99 - inválidos
24:70 - inválidos
60:00 - inválidos
00:00 - válidos
00:01 - válidos
20:00 - válidos
23:59 - válidos
23:99 - inválidos
24:70 - inválidos
60:00 - inválidos
/^([01]\d|2[0-3]):[0-5]\d$/
Aplicação
RegExp.prototype.test()
let message = 'ifpb';
let pattern = /\w+/;
console.log(pattern.test(message)); //=> true
let message = 'ifpb';
let pattern = /\w+/;
console.log(pattern.test(message)); //=> true
String.prototype.split()
let message = 'lorem ipsum\ndolor';
let pattern = /\s/g;
console.log(message.split(pattern)); //=> [('lorem', 'ipsum', 'dolor')];
let message = 'lorem ipsum\ndolor';
let pattern = /\s/g;
console.log(message.split(pattern)); //=> [('lorem', 'ipsum', 'dolor')];
String.prototype.replace()
let cep = '12.345-678';
console.log(cep.replace(/[\.-]/g, '')); //=> 12345678;
let cep = '12.345-678';
console.log(cep.replace(/[\.-]/g, '')); //=> 12345678;
HTML input pattern
<input type="text" pattern="\d{5}-\d{3}" />
<input type="text" pattern="\d{5}-\d{3}" />