Skip to content

Commit 1067461

Browse files
authored
Merge pull request #420 from Peruibeloko/09/04
Anchors: string start ^ and end $
2 parents cb679e1 + 56d7f23 commit 1067461

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
An empty string is the only match: it starts and immediately finishes.
1+
Uma string vazia é a única correspondência: Ela começa e imediatamente termina.
22

3-
The task once again demonstrates that anchors are not characters, but tests.
3+
Esta tarefa demostra novamente que âncoras não são caracteres, mas sim testes.
44

5-
The string is empty `""`. The engine first matches the `pattern:^` (input start), yes it's there, and then immediately the end `pattern:$`, it's here too. So there's a match.
5+
A string está vazia `""`. O interpretador primeiro encontra o padrão de início da entrada `pattern:^` (que sim, está lá), e então imediatamente o fim da entrada `pattern:$` (que também está). Temos uma correspondência.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Regexp ^$
1+
# Expressão Regular ^$
22

3-
Which string matches the pattern `pattern:^$`?
3+
Qual string corresponde ao padrão `pattern:^$`?
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
# Anchors: string start ^ and end $
1+
# Âncoras: início ^ e fim $ de string
22

3-
The caret `pattern:^` and dollar `pattern:$` characters have special meaning in a regexp. They are called "anchors".
3+
Os caracteres acento circunflexo `pattern:^` e cifrão `pattern:$` possuem um significado especial em expressões regulares. Eles são chamados de "âncoras".
44

5-
The caret `pattern:^` matches at the beginning of the text, and the dollar `pattern:$` -- at the end.
5+
O acento circunflexo `pattern:^` corresponde ao início da string, e o cifrão `pattern:$` ao final.
66

7-
For instance, let's test if the text starts with `Mary`:
7+
Neste exemplo, vamos testar se o texto começa com `Maria`:
88

99
```js run
10-
let str1 = "Mary had a little lamb";
11-
alert( /^Mary/.test(str1) ); // true
10+
let str1 = "Maria tinha um cordeirinho";
11+
alert( /^Maria/.test(str1) ); // true
1212
```
1313

14-
The pattern `pattern:^Mary` means: "string start and then Mary".
14+
O padrão `pattern:^Maria` quer dizer: "início da string, e então Maria"
1515

16-
Similar to this, we can test if the string ends with `snow` using `pattern:snow$`:
16+
Da mesma maneira, podemos testar se a string termina com `neve` usando `pattern:neve$`:
1717

1818
```js run
19-
let str1 = "its fleece was white as snow";
20-
alert( /snow$/.test(str1) ); // true
19+
let str1 = "Seu velo era branco como a neve";
20+
alert( /neve$/.test(str1) ); // true
2121
```
2222

23-
In these particular cases we could use string methods `startsWith/endsWith` instead. Regular expressions should be used for more complex tests.
23+
Nesses casos em particular, poderíamos usar os métodos do objeto string `startsWith/endsWith` em seu lugar. Expressões regulares devem ser usadas para testes mais complexos.
2424

25-
## Testing for a full match
25+
## Correspondendo com uma string inteira
2626

27-
Both anchors together `pattern:^...$` are often used to test whether or not a string fully matches the pattern. For instance, to check if the user input is in the right format.
27+
Frequentemente, ambas as âncoras `pattern:^...$` são usadas juntas para verificar se uma string inteira corresponde ao padrão. Para confirmar, por exemplo, se a entrada do usuário está no formato correto.
2828

29-
Let's check whether or not a string is a time in `12:34` format. That is: two digits, then a colon, and then another two digits.
29+
Vamos verificar se uma string é um horário no formato `12:34`. Isto é: dois dígitos, seguido de dois pontos (':'), e então mais dois dígitos.
3030

31-
In regular expressions language that's `pattern:\d\d:\d\d`:
31+
Em expressões regulares, isso fica `pattern:\d\d:\d\d`:
3232

3333
```js run
3434
let goodInput = "12:34";
@@ -39,14 +39,14 @@ alert( regexp.test(goodInput) ); // true
3939
alert( regexp.test(badInput) ); // false
4040
```
4141

42-
Here the match for `pattern:\d\d:\d\d` must start exactly after the beginning of the text `pattern:^`, and the end `pattern:$` must immediately follow.
42+
Nesse exemplo, a correspondência com o padrão `pattern:\d\d:\d\d` deve iniciar exatamente após o início da string `pattern:^`, e ser seguido imediatamente pelo fim da string `pattern:$`.
4343

44-
The whole string must be exactly in this format. If there's any deviation or an extra character, the result is `false`.
44+
A string inteira deve obedecer exatamente a esse formato. Se houver qualquer desvio ou caractere adicional, o resultado será `false`.
4545

46-
Anchors behave differently if flag `pattern:m` is present. We'll see that in the next article.
46+
Âncoras tem um comportamento diferente caso a flag `pattern:m` esteja presente. Veremos isso no próximo artigo.
4747

48-
```smart header="Anchors have \"zero width\""
49-
Anchors `pattern:^` and `pattern:$` are tests. They have zero width.
48+
```smart header="Âncoras tem \"largura zero\""
49+
As âncoras `pattern:^` e `pattern:$` são verificações. Elas não possuem largura.
5050
51-
In other words, they do not match a character, but rather force the regexp engine to check the condition (text start/end).
51+
Em outras palavras, elas não correspondem com um caractere, mas sim com uma posição, obrigando o interpretador regex a verificar a condição de início ou fim da string.
5252
```

0 commit comments

Comments
 (0)