--- id: 587d7dbb367417b2b2512baa title: Reuse Patterns Using Capture Groups challengeType: 1 videoUrl: '' localeTitle: Reutilizar padrões usando grupos de captura --- ## Description
Alguns padrões que você procura irão ocorrer várias vezes em uma string. É um desperdício repetir manualmente esse regex. Existe uma maneira melhor de especificar quando você tem várias substrings de repetição em sua string. Você pode pesquisar substrings de repetição usando 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";
deixe o repeatRegex = / (\ w +) \ s \ 1 /;
repeatRegex.test (repeatStr); // Retorna true
repeatStr.match (repeatRegex); // Retorna ["regex regex", "regex"]
Usar o método .match() em uma string retornará uma matriz com a string correspondente, junto com seu grupo de captura.
## Instructions
Use 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.
## Tests
```yml tests: - text: Seu regex deve usar a classe de caractere abreviada para dígitos. testString: 'assert(reRegex.source.match(/\\d/), "Your regex should use the shorthand character class for digits.");' - text: Seu regex deve reutilizar o grupo de captura duas vezes. testString: 'assert(reRegex.source.match(/\\\d/g).length === 2, "Your regex should reuse the capture group twice.");' - text: Seu regex deve ter dois espaços separando os três números. testString: 'assert(reRegex.source.match(/\\s/g).length === 2, "Your regex should have two spaces separating the three numbers.");' - text: Seu regex deve corresponder "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".");' ```
## Challenge Seed
```js let repeatNum = "42 42 42"; let reRegex = /change/; // Change this line let result = reRegex.test(repeatNum); ```
## Solution
```js // solution required ```