Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Patterns and flags#147

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Changes fromall commits
Commits
Show all changes
14 commits
Select commitHold shift + click to select a range
efa4871
Deleted file that aren't updated with english version
fsdudeMay 28, 2020
b707c26
Retranslation of the updated english version done
fsdudeMay 28, 2020
b45e0db
Merge branch 'master' into 01-regexp-introduction
odsantosDec 24, 2020
e3fbf39
Update 9-regular-expressions/01-regexp-introduction/article.md
odsantosJul 14, 2021
2e57ce5
Update 9-regular-expressions/01-regexp-introduction/article.md
odsantosJul 14, 2021
a27c352
Update 9-regular-expressions/01-regexp-introduction/article.md
odsantosJul 14, 2021
7481795
Update 9-regular-expressions/01-regexp-introduction/article.md
odsantosJul 14, 2021
9166ab4
Update 9-regular-expressions/01-regexp-introduction/article.md
odsantosJul 14, 2021
c061b7a
Update 9-regular-expressions/01-regexp-introduction/article.md
odsantosJul 14, 2021
4f5464d
Update 9-regular-expressions/01-regexp-introduction/article.md
odsantosJul 14, 2021
3b1b03f
Update 9-regular-expressions/01-regexp-introduction/article.md
odsantosJul 14, 2021
48e7e8c
Update 9-regular-expressions/01-regexp-introduction/article.md
odsantosJul 14, 2021
d3d72e3
Update 9-regular-expressions/01-regexp-introduction/article.md
odsantosJul 14, 2021
91686e7
refactor: undo translations that shouldn't happen
jonnathan-lsJan 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 81 additions & 81 deletions9-regular-expressions/01-regexp-introduction/article.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,177 +1,177 @@
#Patterns and flags
#Padrões e flags

Regular expressions are patterns that provide a powerful way to search and replace in text.
Expressões regulares são padrões que fornecem uma maneira poderosa de pesquisar e substituir no texto.

In JavaScript,they are available via the[RegExp](mdn:js/RegExp) object, as well as being integrated in methods of strings.
Em JavaScript,elas estão disponíveis através do objeto[RegExp](mdn:js/RegExp), além de estarem integradas em métodos de strings.

##Regular Expressions
##Expressões regulares

Aregularexpression (also "regexp", or just "reg")consists of a *pattern* and optional*flags*.
Uma expressãoregular(também "regexp" ou apenas "reg")consiste em um *padrão* e*flags* opcionais.

There are two syntaxes that can be used to create a regular expression object.
Existem duas sintaxes que podem ser usadas para criar um objeto de expressão regular.

The "long" syntax:
A sintaxe "longa":

```js
regexp = new RegExp("pattern", "flags");
regexp = new RegExp("padrão", "flags");
```

And the "short" one, using slashes `"/"`:
E a "curta", usando barras `"/"`:

```js
regexp = /pattern/; //no flags
regexp = /pattern/gmi; //with flagsg,m and i (to be covered soon)
regexp = /padrão/; //sem flags
regexp = /padrão/gmi; //com flags`g`, `m` e `i` (a ser abordado em breve)
```

Slashes `pattern:/.../`tellJavaScriptthat we are creating a regular expression. They play the same role as quotes for strings.
Barras `pattern:/.../`informam aoJavaScriptque estamos criando uma expressão regular. Eles desempenham o mesmo papel que aspas para strings.

In both cases`regexp`becomes an instance of the built-in`RegExp` class.
Em ambos os casos, a`regexp`se torna numa instância da classe interna`RegExp`.

The main difference between these two syntaxes is that pattern using slashes`/.../`does not allow for expressions to be inserted (like stringtemplate literals with`${...}`).They are fully static.
A principal diferença entre essas duas sintaxes é que o padrão usando barras`/.../`não permite a inserção de expressões (como modelos de stringliterais com`${...}`).Eles são totalmente estáticos.

Slashes are used when we know theregularexpression at the code writing time -- and that's the most common situation. While `new RegExp`is more often used when we need to create aregexp "on the fly" from a dynamically generatedstring. For instance:
As barras são usadas quando conhecemos a expressãoregularno momento da escrita do código - e essa é a situação mais comum. Enquanto `new RegExp`é mais frequentemente usada quando precisamos criar umaregexp "de improviso" a partir de umastring gerada dinamicamente. Por exemplo:

```js
let tag = prompt("What tagdo you want to find?", "h2");
let tag = prompt("Qual tagvocê deseja encontrar?", "h2");

let regexp = new RegExp(`<${tag}>`); //same as /<h2>/if answered "h2"in thepromptabove
let regexp = new RegExp(`<${tag}>`); //igual a /<h2>/se respondeu "h2"nopromptacima
```

## Flags

Regular expressions may have flagsthat affect the search.
Expressões regulares podem ter flagsque afetam a pesquisa.

There are only 6 of them in #"9f1f49f13800363ac0c70439f5395ee131289667ca0541040cb2e1f975433ca8">Existem apenas 6 delas em #"9f1f49f13800363ac0c70439f5395ee131289667ca0541040cb2e1f975433ca8">
`pattern:i`
:With this flag the search is case-insensitive: no difference between`A`and `a` (see the example below).
:Com essa flag, a pesquisa não diferencia maiúsculas de minúsculas: não há diferença entre`A`e `a` (veja o exemplo abaixo).

`pattern:g`
:With this flag the search looks for all matches, without it -- only the first match is returned.
:Com essa flag, a pesquisa procura todas as correspondências, sem ela - somente a primeira correspondência é retornada.

`pattern:m`
:Multiline mode (covered in the chapter <info:regexp-multiline-mode>).
:Modo multilinha (abordado no capítulo <info:regexp-multiline-mode>).

`pattern:s`
:Enables"dotall" mode, that allows a dot`pattern:.`to match newline character`\n` (covered in the chapter <info:regexp-character-classes>).
:Ativa o modo"dotall", que permite que um ponto`pattern:.`corresponda ao caractere de nova linha`\n` (abordado no capítulo <info:regexp-character-classes>).

`pattern:u`
:Enables full Unicode support. The flagenables correct processing of surrogate pairs. More about that in the chapter <info:regexp-unicode>.
:Ativa o suporte completo de Unicode. A flagpermite o processamento correto de pares substitutos. Mais sobre isso no capítulo <info:regexp-unicode>.

`pattern:y`
:"Sticky" mode: searching at the exact position in the text (covered in the chapter <info:regexp-sticky>)
:Modo "fixo": pesquisando na posição exata no texto (abordado no capítulo <info:regexp-sticky>)

```smart header="Colors"
From here on the color scheme is:
```smart header="Cores"
A partir daqui, o esquema de cores é:

- regexp -- `pattern:red`
- string (where we search) -- `subject:blue`
- result -- `match:green`
- regexp -- `pattern:vermelho`
- string (onde pesquisamos) -- `subject:azul`
- result -- `match:verde`
```

##Searching: str.match
##Pesquisando: str.match

As mentioned previously, regular expressions are integrated with string methods.
Como mencionado anteriormente, expressões regulares são integradas a métodos de string.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
Como mencionado anteriormente, expressões regulares são integradas a métodos de string.
Como mencionado anteriormente, expressões regulares são integradas a métodos de string.
Suggested change
Como mencionado anteriormente, expressões regularessãointegradasa métodos de string.
Como mencionado anteriormente, expressões regularespodem serintegradasem métodos de string.


The method `str.match(regexp)`finds all matches of`regexp`in the string `str`.
O método `str.match(regexp)`encontra todas as correspondências de`regexp`na string `str`.

It has 3 working modes:
Possui 3 modos de trabalho:

1.If theregularexpression hasflag `pattern:g`,it returns an array of all matches:
1.Se a expressãoregulartiverflag `pattern:g`,ela retornará uma matriz de todas as correspondências:
```js run
let str = "We will, we will rock you";
let str = "Nós vamos, nós vamos sacudir você";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
let str = "Nós vamos, nós vamossacudir você";
let str = "Nós vamos, nós vamosbaloiçar você";


alert( str.match(/we/gi) ); //We,we (an array of 2 substringsthat match)
alert( str.match(/nós/gi) ); //Nós, nós (uma matriz de 2 substringsque correspondem)
```
Please note that both`match:We` and `match:we` are found, because flag `pattern:i`makes theregularexpression case-insensitive.
Observe que ambas`match:Nós` e `match:nós` são encontradas, porque flag `pattern:i`torna a expressãoregularsem distinção entre maiúsculas e minúsculas.

2.If there's no such flag it returns only the first match in the form of an array, with the full match at index `0`and some additional details in properties:
2.Se não houver essa flag, ela retornará apenas a primeira correspondência na forma de uma matriz, com a correspondência completa no índice `0`e alguns detalhes adicionais nas propriedades:
```js run
let str = "We will, we will rock you";
let str = "Nós vamos, nós vamos sacudir você";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
let str = "Nós vamos, nós vamossacudir você";
let str = "Nós vamos, nós vamosbaloiçar você";


let result = str.match(/we/i); //without flag g
let result = str.match(/nós/i); //sem flag g

alert( result[0] ); //We (1st match)
alert( result[0] ); //Nós (1º correspondência)
alert( result.length ); // 1

//Details:
alert( result.index ); // 0 (position of the match)
alert( result.input ); //We will, we will rock you (sourcestring)
//Detalhes:
alert( result.index ); // 0 (posição da correspondência)
alert( result.input ); //Nós vamos, nós vamos sacudir você (string de origem)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
alert( result.input ); // Nós vamos, nós vamossacudir você (string de origem)
alert( result.input ); // Nós vamos, nós vamosbaloiçar você (string de origem)

```
The array may have other indexes, besides`0`if a part of the regularexpression is enclosed in parentheses. We'll cover that in the chapter <info:regexp-groups>.
A matriz pode ter outros índices, além de`0`se uma parte da expressão regularestiver entre parênteses. Abordaremos isso no capítulo <info:regexp-groups>.

3.And, finally, if there are no matches, `null`is returned (doesn't matter if there's flag `pattern:g`or not).
3.E, finalmente, se não houver correspondências, `null`é retornado (não importa se há flags `pattern:g`ou não).

This a very importantnuance. If there are no matches, we don't receive an empty array, but instead receive`null`.Forgetting about that may lead to errors, e.g.:
Esta é umanuance muito importante. Se não houver correspondências, não receberemos uma matriz vazia, mas receberemos`null`.Esquecer isso pode levar a erros, por exemplo:

```js run
let matches = "JavaScript".match(/HTML/); // = null

if (!matches.length) { // Error: Cannot read property 'length' of null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
if (!matches.length) { // Error: Cannot read property 'length' of null
if (!matches.length) { // Error: Cannot read property 'length' of null (Erro: Não é possível ler a propriedade 'length' de null)

alert("Error in the line above");
alert("Erro na linha acima");
}
```

If we'd like the result to always be an array, we can write it this way:
Se quisermos que o resultado seja sempre uma matriz, podemos escrevê-lo desta maneira:

```js run
let matches = "JavaScript".match(/HTML/)*!* || []*/!*;
let matches = "JavaScript".match(/HTML/) || [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
let matches = "JavaScript".match(/HTML/) || [];
let matches = "JavaScript".match(/HTML/)*!* || []*/!*;


if (!matches.length) {
alert("No matches"); //now it works
alert("Sem correspondências"); //agora funciona
}
```

##Replacing: str.replace
##Substituindo: str.replace

The method `str.replace(regexp,replacement)`replaces matches found using`regexp`in string `str`with `replacement` (all matches if there'sflag `pattern:g`,otherwise, only the first one).
O método `str.replace(regexp,substituição)`substitui as correspondências encontradas usando`regexp`na string `str`por `substituição` (todas as correspondências se houverflag `pattern:g`,caso contrário, apenas a primeira).

For instance:
Por exemplo:

```js run
//no flag g
alert( "We will, we will".replace(/we/i, "I") ); //I will, we will
//sem flag g
alert( "Nós vamos, nós vamos".replace(/nós/i, "Eu") ); //Eu vamos, nós vamos

//with flag g
alert( "We will, we will".replace(/we/ig, "I") ); //I will, I will
//com flag g
alert( "Nós vamos, nós vamos".replace(/nós/ig, "Eu") ); //Eu vamos, Eu vamos
```

The second argument is the `replacement` string. We can use special character combinations in it to insert fragments of the match:
O segundo argumento é a string de `substituição`. Podemos usar combinações especiais de caracteres para inserir fragmentos da correspondência:

|Symbols |Action in the replacement string |
|Símbolos |Ação na string de substituição |
|--------|--------|
|`$&`|inserts the whole match|
|<code>$&#096;</code>|inserts a part of thestringbefore the match|
|`$'`|inserts a part of thestringafter the match|
|`$n`|if `n`is a1-2digit number, then it inserts the contents of n-th parentheses, more about it in the chapter <info:regexp-groups>|
|`$<name>`|inserts the contents of the parentheses with the given `name`, more about it in the chapter <info:regexp-groups>|
|`$$`|inserts character `$` |
|`$&`|insere toda a correspondência|
|<code>$&#096;</code>|insere uma parte dastringantes da correspondência|
|`$'`|insere uma parte dastringdepois da correspondência|
|`$n`|se `n`for um número de1-2dígitos, ela inserirá o conteúdo dos enésimos parênteses, mais sobre isso no capítulo <info:regexp-groups>|
|`$<nome>`|insere o conteúdo dos parênteses com o `nome` fornecido, mais sobre isso no capítulo <info:regexp-groups>|
|`$$`|insere o caractere `$` |

An example with `pattern:$&`:
Um exemplo com o `pattern:$&`:

```js run
alert( "I love HTML".replace(/HTML/, "$&and JavaScript") ); //I love HTMLand JavaScript
alert( "Eu amo HTML".replace(/HTML/, "$&e JavaScript") ); //Eu amo HTMLe JavaScript
```

##Testing: regexp.test
##Teste: regexp.test

The method `regexp.test(str)`looks for at least one match, if found, returns `true`,otherwise`false`.
O método `regexp.test(str)`procura pelo menos uma correspondência, se encontrada, retorna `true`,caso contrário,`false`.

```js run
let str = "I love JavaScript";
let regexp = /LOVE/i;
let str = "Eu amo JavaScript";
let regexp = /AMO/i;

alert( regexp.test(str) ); // true
```

Later in this chapter we'll study more regular expressions, walk through more examples, and also meet other methods.
Mais adiante neste capítulo, estudaremos mais expressões regulares, examinaremos mais exemplos e também conheceremos outros métodos.

Full information about the methods is given in the article <info:regexp-methods>.
Informações completas sobre os métodos são fornecidas no artigo <info:regexp-methods>.

##Summary
##Resumo

-Aregularexpression consists of a pattern and optionalflags: `pattern:g`, `pattern:i`, `pattern:m`, `pattern:u`, `pattern:s`, `pattern:y`.
-Without flagsand special symbols (that we'll study later),the search by a regexpis the same as a substring search.
-The method `str.match(regexp)`looks for matches: all of them if there's`pattern:g` flag, otherwise, only the first one.
-The method `str.replace(regexp,replacement)`replaces matches found using`regexp`with `replacement`: all of them if there's`pattern:g` flag, otherwise only the first one.
-The method `regexp.test(str)`returns `true`if there's at least one match, otherwise, it returns `false`.
-Uma expressãoregularconsiste em um padrão eflags opcionais: `pattern:g`, `pattern:i`, `pattern:m`, `pattern:u`, `pattern:s`, `pattern:y`.
-Sem flagse símbolos especiais (que estudaremos mais adiante),a pesquisa por uma regexpé igual à pesquisa com substring.
-O método `str.match(regexp)`procura por correspondências: todas elas se houver a flag`pattern:g`, caso contrário, apenas a primeira.
-O método `str.replace(regexp,substituição)`substitui as correspondências encontradas usando`regexp`por 'substituição': todas elas se houver uma flag`pattern:g`, caso contrário, somente a primeira.
-O método `regexp.test(str)`retorna `true`se houver pelo menos uma correspondência, caso contrário, retorna `false`.

[8]ページ先頭

©2009-2025 Movatter.jp