-
-
Notifications
You must be signed in to change notification settings - Fork 113
Escaping, special characters #425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,99 +1,99 @@ | ||||||
| # Escapes, caracteres especiais | ||||||
|
|
||||||
| # Escaping, special characters | ||||||
| Como vimos anteriormente, a contrabarra `pattern:\` é usada para demarcar classes de caracteres, como `pattern:\d` por exemplo. Sendo então, um caractere especial em regexes (bem como em strings normais). | ||||||
|
|
||||||
| As we've seen, a backslash `pattern:\` is used to denote character classes, e.g. `pattern:\d`. So it's a special character in regexps (just like in regular strings). | ||||||
| Existem outros caracteres especiais que também tem significados especias numa regex, como `pattern:[ ] { } ( ) \ ^ $ . | ? * +`. Esses são usados para realizar buscas mais poderosas. | ||||||
|
|
||||||
| There are other special characters as well, that have special meaning in a regexp, such as `pattern:[ ] { } ( ) \ ^ $ . | ? * +`. They are used to do more powerful searches. | ||||||
| Não tente decorar a lista -- em breve vamos cobrir cada um deles, e no processo você irá decorá-los automaticamente. | ||||||
|
|
||||||
| Don't try to remember the list -- soon we'll deal with each of them, and you'll know them by heart automatically. | ||||||
| ## Escapes | ||||||
|
|
||||||
| ## Escaping | ||||||
| Digamos que precisamos buscar um ponto '.' literal. Não "qualquer caractere", apenas um ponto. | ||||||
|
|
||||||
| Let's say we want to find literally a dot. Not "any character", but just a dot. | ||||||
| Para usar um caractere especial como um caractere comum, adicionamos uma contrabarra dessa forma: `pattern:\.`. | ||||||
|
|
||||||
| To use a special character as a regular one, prepend it with a backslash: `pattern:\.`. | ||||||
| Isso também é conhecido como "escapar um caractere". | ||||||
|
|
||||||
| That's also called "escaping a character". | ||||||
| Por exemplo: | ||||||
|
|
||||||
| For example: | ||||||
| ```js run | ||||||
| alert( "Chapter 5.1".match(/\d\.\d/) ); // 5.1 (match!) | ||||||
| alert( "Chapter 511".match(/\d\.\d/) ); // null (looking for a real dot \.) | ||||||
| alert("Chapter 5.1".match(/\d\.\d/)); // 5.1 (match!) | ||||||
| alert("Chapter 511".match(/\d\.\d/)); // null (procurando por um ponto literal \.) | ||||||
| ``` | ||||||
|
|
||||||
| Parentheses are also special characters, so if we want them, we should use `pattern:\(`. The example below looks for a string `"g()"`: | ||||||
| Parênteses também são caracteres especiais, então se quisermos usá-los, devemos usar `pattern:\(`. O exemplo abaixo procura a string `"g()"`: | ||||||
|
|
||||||
| ```js run | ||||||
| alert( "function g()".match(/g\(\)/) ); // "g()" | ||||||
| alert("function g()".match(/g\(\)/)); // "g()" | ||||||
| ``` | ||||||
|
|
||||||
| If we're looking for a backslash `\`, it's a special character in both regular strings and regexps, so we should double it. | ||||||
| Se estivermos buscando por uma contrabarra `\`, que é um caractere especial tanto em strings comuns quanto em regexes, devemos escapá-la também. | ||||||
|
|
||||||
| ```js run | ||||||
| alert( "1\\2".match(/\\/) ); // '\' | ||||||
| alert("1\\2".match(/\\/)); // '\' | ||||||
| ``` | ||||||
|
|
||||||
| ## A slash | ||||||
| ## Uma barra | ||||||
|
|
||||||
| A slash symbol `'/'` is not a special character, but in JavaScript it is used to open and close the regexp: `pattern:/...pattern.../`, so we should escape it too. | ||||||
| O caractere de barra `'/'` não é um caractere especial, mas no JavaScript é usado para delimitar regexes: `pattern:/...pattern.../`, então devemos escapá-la também. | ||||||
|
|
||||||
| Here's what a search for a slash `'/'` looks like: | ||||||
| Uma busca por uma barra `'/'` fica assim: | ||||||
|
|
||||||
| ```js run | ||||||
| alert( "/".match(/\//) ); // '/' | ||||||
| alert("/".match(/\//)); // '/' | ||||||
| ``` | ||||||
|
|
||||||
| On the other hand, if we're not using `pattern:/.../`, but create a regexp using `new RegExp`, then we don't need to escape it: | ||||||
| Por outro lado, se não estivermos usando a sintaxe `pattern:/.../`, mas sim o construtor `new RegExp`, não é necessário usar um escape: | ||||||
|
|
||||||
| ```js run | ||||||
| alert( "/".match(new RegExp("/")) ); // finds / | ||||||
| alert("/".match(new RegExp("/"))); // busca um / | ||||||
| ``` | ||||||
|
|
||||||
| ## new RegExp | ||||||
|
|
||||||
| If we are creating a regular expression with `new RegExp`, then we don't have to escape `/`, but need to do some other escaping. | ||||||
| Se estivermos criando uma expressão regular com o `new RegExp`, não precisamos escapar o `/`, mas precisamos aplicar outros escapes. | ||||||
|
|
||||||
| For instance, consider this: | ||||||
| Considere esse exemplo: | ||||||
|
|
||||||
| ```js run | ||||||
| let regexp = new RegExp("\d\.\d"); | ||||||
| let regexp = new RegExp("d.d"); | ||||||
|
|
||||||
| alert( "Chapter 5.1".match(regexp) ); // null | ||||||
| alert("Chapter 5.1".match(regexp)); // null | ||||||
| ``` | ||||||
|
|
||||||
| The similar search in one of previous examples worked with `pattern:/\d\.\d/`, but `new RegExp("\d\.\d")` doesn't work, why? | ||||||
| Uma busca muito similar em um dos exemplos anteriores funcionou com o `pattern:/\d\.\d/`, mas nosso `new RegExp("\d\.\d")` não. Por quê? | ||||||
|
|
||||||
| The reason is that backslashes are "consumed" by a string. As we may recall, regular strings have their own special characters, such as `\n`, and a backslash is used for escaping. | ||||||
| Isso acontece porque contrabarras são "consumidas" pela string. Como deve se lembrar, strings comuns tem seus próprios caracteres especiais, como o `\n`, e contrabarras são usadas para escapes. | ||||||
|
|
||||||
| Here's how "\d\.\d" is perceived: | ||||||
| Veja como "\d\.\d" é interpretado: | ||||||
|
|
||||||
| ```js run | ||||||
| alert("\d\.\d"); // d.d | ||||||
| alert("d.d"); // d.d | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aqui aconteceu de novo:
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| String quotes "consume" backslashes and interpret them on their own, for instance: | ||||||
| Strings comuns "consomem" contrabarras e interpretam-nas separadamente, por exemplo: | ||||||
|
|
||||||
| - `\n` -- becomes a newline character, | ||||||
| - `\u1234` -- becomes the Unicode character with such code, | ||||||
| - ...And when there's no special meaning: like `pattern:\d` or `\z`, then the backslash is simply removed. | ||||||
| - `\n` -- se torna um caractere de quebra de linha, | ||||||
| - `\u1234` -- se torna o caractere Unicode com o código fornecido, | ||||||
| - ...E quando não há nenhum significado especial, como `pattern:\d` ou `\z`, a contrabarra é simplesmente removida. | ||||||
|
|
||||||
| So `new RegExp` gets a string without backslashes. That's why the search doesn't work! | ||||||
| Então `new RegExp` recebe uma string sem contrabarras. Por isso que a busca não funciona! | ||||||
|
|
||||||
| To fix it, we need to double backslashes, because string quotes turn `\\` into `\`: | ||||||
| Para consertar isso, precisamos escapar a contrabarra, já que strings comuns tornam `\\` em `\`: | ||||||
|
|
||||||
| ```js run | ||||||
| *!* | ||||||
| let regStr = "\\d\\.\\d"; | ||||||
| */!* | ||||||
| alert(regStr); // \d\.\d (correct now) | ||||||
| alert(regStr); // \d\.\d (correto) | ||||||
|
|
||||||
| let regexp = new RegExp(regStr); | ||||||
|
|
||||||
| alert( "Chapter 5.1".match(regexp) ); // 5.1 | ||||||
| ``` | ||||||
|
|
||||||
| ## Summary | ||||||
| ## Resumo | ||||||
|
|
||||||
| - To search for special characters `pattern:[ \ ^ $ . | ? * + ( )` literally, we need to prepend them with a backslash `\` ("escape them"). | ||||||
| - We also need to escape `/` if we're inside `pattern:/.../` (but not inside `new RegExp`). | ||||||
| - When passing a string to `new RegExp`, we need to double backslashes `\\`, cause string quotes consume one of them. | ||||||
| - Para usar caracteres especiais `pattern:[ \ ^ $ . | ? * + ( )` de maneira literal, precisamos usar a contrabarra `\` ("escapar os caracteres"). | ||||||
| - Nós também precisamos escapar a `/` se estivermos dentro do `pattern:/.../` (mas não do `new RegExp`). | ||||||
| - Quando passarmos uma string para `new RegExp`, precisamos escapar contrabarras, (`\\`) já que strings consomem uma delas. | ||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Me parece que aqui houve uma alteração no exemplo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gabifs, the priority is to conclude the translation first, and then update the content to be current as the English original content.