capture groups
. Parênteses, (
e )
, são usados para encontrar substrings de repetição. Você coloca o regex do padrão que será repetido entre os parênteses. Para especificar onde essa sequência de repetição aparecerá, use uma barra invertida ( \
) e, em seguida, um número. Esse número começa em 1 e aumenta com cada grupo de captura adicional usado. Um exemplo seria \1
para corresponder ao primeiro grupo. O exemplo abaixo corresponde a qualquer palavra que ocorre duas vezes separada por um espaço: let repeatStr = "regex regex";Usar o método
deixe o repeatRegex = / (\ w +) \ s \ 1 /;
repeatRegex.test (repeatStr); // Retorna true
repeatStr.match (repeatRegex); // Retorna ["regex regex", "regex"]
.match()
em uma string retornará uma matriz com a string correspondente, junto com seu grupo de captura. capture groups
em reRegex
para corresponder números que são repetidos apenas três vezes em uma cadeia, cada um separado por um espaço. "42 42 42"
.
testString: 'assert(reRegex.test("42 42 42"), "Your regex should match "42 42 42"
.");'
- text: Seu regex deve corresponder a "100 100 100"
.
testString: 'assert(reRegex.test("100 100 100"), "Your regex should match "100 100 100"
.");'
- text: Seu regex não deve corresponder a "42 42 42 42"
.
testString: 'assert.equal(("42 42 42 42").match(reRegex.source), null, "Your regex should not match "42 42 42 42"
.");'
- text: Seu regex não deve corresponder a "42 42"
.
testString: 'assert.equal(("42 42").match(reRegex.source), null, "Your regex should not match "42 42"
.");'
- text: Seu regex não deve coincidir com "101 102 103"
.
testString: 'assert(!reRegex.test("101 102 103"), "Your regex should not match "101 102 103"
.");'
- text: Seu regex não deve corresponder a "1 2 3"
.
testString: 'assert(!reRegex.test("1 2 3"), "Your regex should not match "1 2 3"
.");'
- text: Seu regex deve coincidir com "10 10 10"
.
testString: 'assert(reRegex.test("10 10 10"), "Your regex should match "10 10 10"
.");'
```