Skip to content

Multiline mode of anchors ^ $, flag "m" #259

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 8 commits into from
Mar 15, 2023
Merged
Changes from all commits
Commits
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
48 changes: 24 additions & 24 deletions 9-regular-expressions/05-regexp-multiline-mode/article.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Multiline mode of anchors ^ $, flag "m"
# حالت چند خطی anchors ^ $، flag "m"

The multiline mode is enabled by the flag `pattern:m`.
حالت چند خطی با flag `pattern:m` فعال می شود.

It only affects the behavior of `pattern:^` and `pattern:$`.
فقط بر رفتار `^:pattern` و `$:pattern` تأثیر می گذارد.

In the multiline mode they match not only at the beginning and the end of the string, but also at start/end of line.
در حالت چند خطی، نه تنها در ابتدا و انتهای رشته، بلکه در شروع/پایان خط نیز مطابقت دارند.

## Searching at line start ^
## جستجو در شروع خط ^

In the example below the text has multiple lines. The pattern `pattern:/^\d/gm` takes a digit from the beginning of each line:
در مثال زیر متن دارای چندین خط است. الگوی `pattern:/^\d/gm` از ابتدای هر خط یک رقم می گیرد:

```js run
let str = `1st place: Winnie
Expand All @@ -20,7 +20,7 @@ console.log( str.match(/^\d/gm) ); // 1, 2, 3
*/!*
```

Without the flag `pattern:m` only the first digit is matched:
بدون flag `pattern:m` تنها رقم اول مطابقت دارد:

```js run
let str = `1st place: Winnie
Expand All @@ -32,19 +32,19 @@ console.log( str.match(/^\d/g) ); // 1
*/!*
```

That's because by default a caret `pattern:^` only matches at the beginning of the text, and in the multiline mode -- at the start of any line.
دلیلش این است که به طور پیش‌ فرض یک `^:pattern` فقط در ابتدای متن و در حالت چند خطی - در ابتدای هر خط مطابقت دارد.

```smart
"Start of a line" formally means "immediately after a line break": the test `pattern:^` in multiline mode matches at all positions preceded by a newline character `\n`.
"شروع یک خط" به طور رسمی به معنای "بلافاصله پس از شکست خط" است: آزمایش "^:pattern" در حالت چند خطی با همه موقعیت هایی که بعد از یک کاراکترِ خط جدید `n\` قرار دارند مطابقت دارد.

And at the text start.
و در شروع متن.
```

## Searching at line end $
## جستجو در انتهای خط $

The dollar sign `pattern:$` behaves similarly.
علامت دلار `$:pattern` رفتار مشابهی دارد.

The regular expression `pattern:\d$` finds the last digit in every line
عبارت منظم `$pattern:\d` آخرین رقم را در هر خط پیدا می کند

```js run
let str = `Winnie: 1
Expand All @@ -54,21 +54,21 @@ Eeyore: 3`;
console.log( str.match(/\d$/gm) ); // 1,2,3
```

Without the flag `pattern:m`, the dollar `pattern:$` would only match the end of the whole text, so only the very last digit would be found.
بدون flag `pattern:m` و `$:pattern` علامت دلار فقط با انتهای کل متن مطابقت دارد، بنابراین فقط آخرین رقم پیدا می‌ شود.

```smart
"End of a line" formally means "immediately before a line break": the test `pattern:$` in multiline mode matches at all positions succeeded by a newline character `\n`.
"پایان یک خط" به طور رسمی به معنای "بلافاصله قبل از شکست خط" است: تست "$:pattern" در حالت چند خطی با همه موقعیت‌هایی که قبل از یک کاراکتر خط جدید "n\" قرار دارند، مطابقت دارد.

And at the text end.
و در انتهای متن.
```

## Searching for \n instead of ^ $
## جستجو برای \n به جای ^ $

To find a newline, we can use not only anchors `pattern:^` and `pattern:$`, but also the newline character `\n`.
برای یافتن یک خط جدید، می‌ توانیم نه تنها از anchorهای `^:pattern` و `$:pattern`، بلکه از کاراکتر خط جدید `n\` استفاده کنیم.

What's the difference? Let's see an example.
تفاوت در چیست؟ بیایید یک مثال را ببینیم.

Here we search for `pattern:\d\n` instead of `pattern:\d$`:
در اینجا ما `pattern:\d\n` را به جای `$pattern:\d` جستجو می کنیم:

```js run
let str = `Winnie: 1
Expand All @@ -78,10 +78,10 @@ Eeyore: 3`;
console.log( str.match(/\d\n/g) ); // 1\n,2\n
```

As we can see, there are 2 matches instead of 3.
همانطور که می بینیم به جای 3 شباهت 2 شباهت وجود دارد.

That's because there's no newline after `subject:3` (there's text end though, so it matches `pattern:$`).
دلیلش این است که بعد از `3:subject` خط جدیدی وجود ندارد (البته پایان متن وجود دارد، بنابراین با `$:pattern` مطابقت دارد.

Another difference: now every match includes a newline character `match:\n`. Unlike the anchors `pattern:^` `pattern:$`, that only test the condition (start/end of a line), `\n` is a character, so it becomes a part of the result.
تفاوت دیگر: اکنون هر تطابق شامل یک کاراکتر خط جدید `match:\n` است. برخلاف `$:pattern` و `^:pattern` که فقط شرط (شروع/پایان یک خط) را آزمایش می‌ کنند، `n\` یک کاراکتر است، بنابراین بخشی از نتیجه می‌ شود.

So, a `\n` in the pattern is used when we need newline characters in the result, while anchors are used to find something at the beginning/end of a line.
بنابراین، زمانی که به کاراکترهای خط جدید در نتیجه نیاز داشته باشیم، از `n\` در الگو استفاده می‌ شود، در حالی که از anchorها برای یافتن چیزی در ابتدا/پایان یک خط استفاده می‌ شود.