- Notifications
You must be signed in to change notification settings - Fork179
Multiline mode of anchors ^ $, flag "m"#492
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
tarasyyyk merged 3 commits intojavascript-tutorial:masterfrombromotdi:05-regexp-multiline-modeMay 24, 2023
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
72 changes: 36 additions & 36 deletions9-regular-expressions/05-regexp-multiline-mode/article.md
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,87 +1,87 @@ | ||
#Багаторядковий режим якорів^ $,прапора "m" | ||
Багаторядковий режим вмикається прапором `pattern:m`. | ||
Це впливає лише на поведінку`pattern:^`і `pattern:$`. | ||
У багаторядковому режимі вони збігаються не тільки на початку та в кінці тексту, а й на початку/кінці кожного рядка у тексті. | ||
##Пошук на початку рядка ^ | ||
У прикладі нижче текст складається з кількох рядків. Шаблон`pattern:/^\d/gm`бере цифру з початку кожного рядка: | ||
```js run | ||
let str = `1 місце: Вінні-Пух | ||
2 місце: Паць | ||
3 місце: Слонопотам`; | ||
*!* | ||
console.log( str.match(/^\d/gm) ); // 1, 2, 3 | ||
*/!* | ||
``` | ||
Без прапора`pattern:m`збігається лише перша цифра: | ||
```js run | ||
let str = `1 місце: Вінні | ||
2 місце: Паць | ||
3 місце: Слонопотам; | ||
*!* | ||
console.log( str.match(/^\d/g) ); // 1 | ||
*/!* | ||
``` | ||
Це тому, що за замовчуванням карет `pattern:^`збігається лише на початку тексту, а в багаторядковому режимі--на початку будь-якого рядка. | ||
```smart | ||
"Початок рядка" формально означає "відразу після розриву рядка":тестовий`pattern:^`у багаторядковому режимі збігається в усіх позиціях, яким передує символ нового рядка `\n`. | ||
І на початку тексту. | ||
``` | ||
##Пошук у кінці рядка $ | ||
Символ долара`pattern:$`поводиться аналогічно. | ||
Регулярний вираз`pattern:\d$`шукає останню цифру у кожному рядку | ||
```js run | ||
let str = `Вінні: 1 | ||
Паць: 2 | ||
Слонопотам: 3`; | ||
console.log( str.match(/\d$/gm) ); // 1,2,3 | ||
``` | ||
Без прапора`pattern:m`,символ долара `pattern:$`відповідатиме лише кінці всього тексту, тому буде знайдено лише останню цифру. | ||
```smart | ||
"Кінець рядка" формально означає "безпосередньо перед розривом рядка":тестовий`pattern:$`у багаторядковому режимі збігається в усіх позиціях після символу нового рядка `\n`. | ||
І в кінці тексту. | ||
``` | ||
##Шукаємо\nзамість ^ $ | ||
Щоб знайти новий рядок, ми можемо використовувати не лише якорі `pattern:^`і `pattern:$`,а й символ нового рядка `\n`. | ||
У чому різниця? Подивімось на приклад. | ||
Тут ми шукаємо`pattern:\d\n`замість `pattern:\d$`: | ||
```js run | ||
let str = `Вінні: 1 | ||
Паць: 2 | ||
Слонопотам: 3`; | ||
console.log( str.match(/\d\n/g) ); // 1\n,2\n | ||
``` | ||
Як бачимо, 2 збіги замість 3-х. | ||
Це тому, що після `об'єкт:3`немає нового рядка (хоча є кінець тексту, тому він відповідає `pattern:$`). | ||
Ще одна відмінність: тепер кожен збіг містить символ нового рядка `match:\n`.На відміну від якорів`pattern:^` `pattern:$`,які лише перевіряють умову (початок/кінець рядка), `\n`є символом, тому він стає частиною результату. | ||
Отже,`\n`у шаблоні використовується, коли нам потрібні символи нового рядка в результаті, тоді як якорі використовуються, щоб знайти щось на початку/кінці рядка. |
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.