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 | Greedy (* , + , ? , {n} , {n,} , {n, m} ), Non-Greedy (x*? , x+? , x?? , x{n}? , x{n,}? , x{n,m}? ) |
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
/^(\d11|\d3.\d3.\d3-\d2)$/
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
/^(\d8|\d2\.?\d3-\d3)$/
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
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')];
HTML input pattern
<input type="text" pattern="\d{5}-\d{3}" />
<input type="text" pattern="\d{5}-\d{3}" />