2021-06-15 00:49:18 -07:00
|
|
|
---
|
|
|
|
id: 587d7db9367417b2b2512ba6
|
2021-07-21 20:53:20 +05:30
|
|
|
title: Especificar apenas o mínimo de capturas
|
2021-06-15 00:49:18 -07:00
|
|
|
challengeType: 1
|
|
|
|
forumTopicId: 301366
|
|
|
|
dashedName: specify-only-the-lower-number-of-matches
|
|
|
|
---
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
2021-07-16 11:03:16 +05:30
|
|
|
Você pode especificar um número mínimo e um máximo de capturas com chaves. Haverá vezes que você precisará especificar um mínimo mas não um máximo.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
2021-07-16 11:03:16 +05:30
|
|
|
Para fazer isso, apenas escreva o número seguido de uma vírgula dentro das chaves.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
2021-07-16 11:03:16 +05:30
|
|
|
Por exemplo, para capturar pelo menos `3` vezes a letra `a` na string `hah` você pode escrever a regex `/ha{3,}h/`.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
let A4 = "haaaah";
|
|
|
|
let A2 = "haah";
|
|
|
|
let A100 = "h" + "a".repeat(100) + "h";
|
|
|
|
let multipleA = /ha{3,}h/;
|
|
|
|
multipleA.test(A4);
|
|
|
|
multipleA.test(A2);
|
|
|
|
multipleA.test(A100);
|
|
|
|
```
|
|
|
|
|
2021-07-16 11:03:16 +05:30
|
|
|
As três chamadas a `test` acima retornam, na ordem, os valores: `true`, `false` e `true`.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
2021-07-16 11:03:16 +05:30
|
|
|
Modifique a regex `haRegex` para que capture quatro ou mais `z`s na string `Hazzah`.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
2021-07-30 23:57:21 +09:00
|
|
|
A regex deve usar chaves.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(haRegex.source.match(/{.*?}/).length > 0);
|
|
|
|
```
|
|
|
|
|
2021-07-30 23:57:21 +09:00
|
|
|
A regex não deve encontrar a string `Hazzah`
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
2021-10-06 08:36:48 -07:00
|
|
|
haRegex.lastIndex = 0;
|
2021-06-15 00:49:18 -07:00
|
|
|
assert(!haRegex.test('Hazzah'));
|
|
|
|
```
|
|
|
|
|
2021-07-30 23:57:21 +09:00
|
|
|
A regex não deve encontrar a string `Hazzzah`
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
2021-10-06 08:36:48 -07:00
|
|
|
haRegex.lastIndex = 0;
|
2021-06-15 00:49:18 -07:00
|
|
|
assert(!haRegex.test('Hazzzah'));
|
|
|
|
```
|
|
|
|
|
2021-07-30 23:57:21 +09:00
|
|
|
A regex deve encontrar a string `Hazzzzah`
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert('Hazzzzah'.match(haRegex)[0].length === 8);
|
|
|
|
```
|
|
|
|
|
2021-07-30 23:57:21 +09:00
|
|
|
A regex deve encontrar a string `Hazzzzzah`
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert('Hazzzzzah'.match(haRegex)[0].length === 9);
|
|
|
|
```
|
|
|
|
|
2021-07-30 23:57:21 +09:00
|
|
|
A regex deve encontrar a string `Hazzzzzzah`
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert('Hazzzzzzah'.match(haRegex)[0].length === 10);
|
|
|
|
```
|
|
|
|
|
2021-07-30 23:57:21 +09:00
|
|
|
A regex deve capturar 30 `z`s, se presentes, na string `Hazzah`.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert('Hazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzah'.match(haRegex)[0].length === 34);
|
|
|
|
```
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
let haStr = "Hazzzzah";
|
|
|
|
let haRegex = /change/; // Change this line
|
|
|
|
let result = haRegex.test(haStr);
|
|
|
|
```
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
```js
|
|
|
|
let haStr = "Hazzzzah";
|
|
|
|
let haRegex = /Haz{4,}ah/; // Change this line
|
|
|
|
let result = haRegex.test(haStr);
|
|
|
|
```
|