You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As you can see, it matches `match:8`, from `subject:-18`. To exclude it, we need to ensure that the regexp starts matching a number not from the middle of another (non-matching) number.
16
+
كما ترون ، فإنه يطابق `المباراة: 8` ، من` الموضوع: -18`. لاستبعاده ، نحتاج إلى التأكد من أن regexp يبدأ في مطابقة رقم ليس من منتصف رقم آخر (غير مطابق).
17
17
18
-
We can do it by specifying another negative lookbehind: `pattern:(?<!-)(?<!\d)\d+`. Now `pattern:(?<!\d)`ensures that a match does not start after another digit, just what we need.
18
+
يمكننا القيام بذلك عن طريق تحديد مظهر سلبي آخر خلف: `pattern: (؟ <! -) (؟ <! \ d) \ d +`. الآن `النمط: (؟ <! \ d)`يضمن أن المطابقة لا تبدأ بعد رقم آخر ، فقط ما نحتاجه.
19
19
20
-
We can also join them into a single lookbehind here:
20
+
يمكننا أيضًا أن ننضم إليهم في lookbehind خلفنا هنا:
In the replacement string `$&`means the match itself, that is, the part of the source text that corresponds to `pattern:<body.*>`. It gets replaced by itself plus `<h1>Hello</h1>`.
14
+
في السلسلة البديلة `$ &`تعني المطابقة نفسها ، أي جزء النص المصدر الذي يتوافق مع `pattern:<body.*>`. يتم استبداله بمفرده بالإضافة إلى `<h1>Hello</h1>`.
As you can see, there's only lookbehind part in this regexp.
25
+
كما ترون ، هناك فقط جزء وراء النظر في هذا التعبير العادي.
26
26
27
-
It works like this:
28
-
-At every position in the text.
29
-
-Check if it's preceeded by `pattern:<body.*>`.
30
-
-If it's so then we have the match.
27
+
يعمل مثل هذا:
28
+
-في كل موضع في النص.
29
+
-تحقق مما إذا كان مسبوقًا بـ `النمط: <body.*>`.
30
+
-إذا كان الأمر كذلك لدينا المباراة.
31
31
32
-
The tag `pattern:<body.*>` won't be returned. The result of this regexp is literally an empty string, but it matches only at positions preceeded by `pattern:<body.*>`.
32
+
لن يتم إرجاع العلامة `pattern:<body.*>`. نتيجة هذا التعبير العادي هي حرفيا سلسلة فارغة ، لكنها تتطابق فقط في المواضع التي يسبقها `النمط: <body.*>`.
33
33
34
-
So we replaces the "empty line", preceeded by `pattern:<body.*>`, with `<h1>Hello</h1>`. That's the insertion after `<body>`.
34
+
لذلك نستبدل "السطر الفارغ" ، مسبوقًا بـ "pattern:<body.*>` ، بـ `<h1>Hello</h1>`. هذا هو الإدراج بعد "<body>".
35
35
36
-
P.S. Regexp flags, such as `pattern:s`and `pattern:i`can also useful: `pattern:/<body.*>/si`. The `pattern:s`flag makes the dot `pattern:.`match a newline character, and `pattern:i`flag makes `pattern:<body>`also match `match:<BODY>`case-insensitively.
36
+
ملاحظة. علامات Regexp ، مثل `pattern:s`و`pattern:i`يمكن أن تكون مفيدة أيضًا: `pattern: / <body. *> / si`. تجعل علامة `pattern:s`علامة``pattern:.`تتطابق مع حرف سطر جديد ، وعلامة 'pattern:i`تجعل``pattern:<body>`تتطابق أيضًا مع `match:<BODY>` غير حساس لحالة الأحرف.
Sometimes we need to find only those matches for a pattern that are followed or preceeded by another pattern.
3
+
في بعض الأحيان نحتاج إلى العثور فقط على تلك المطابقات لنمط يتبعه أو يسبقه نمط آخر.
4
4
5
-
There's a special syntax for that, called "lookahead" and "lookbehind", together referred to as "lookaround".
5
+
هناك صيغة خاصة لذلك ، تسمى "lookahead" و "lookbehind" ، يشار إليها معًا باسم "lookaround".
6
6
7
-
For the start, let's find the price from the string like `subject:1 turkey costs 30€`. That is: a number, followed by `subject:€` sign.
7
+
في البداية ، دعنا نجد السعر من السلسلة مثل `الموضوع: 1 ديك رومي يكلف 30 يورو`. أي: رقم متبوعًا بعلامة `subject: €`.
8
8
9
9
## Lookahead
10
10
11
-
The syntax is: `pattern:X(?=Y)`, it means "look for `pattern:X`, but match only if followed by `pattern:Y`". There may be any pattern instead of`pattern:X`and`pattern:Y`.
11
+
الصيغة هي: `pattern: X (؟ = Y)` ، وتعني "ابحث عن`pattern: X` ، لكن تطابق فقط إذا تبعها "pattern:Y`". قد يكون هناك أي نمط بدلاً من`pattern:X`و`pattern:Y`.
12
12
13
-
For an integer number followed by `subject:€`, the regexp will be `pattern:\d+(?=€)`:
13
+
بالنسبة لرقم صحيح متبوعًا بـ`subject:€`, سيكون regexp
14
+
`pattern:\d+(?=€)`:
14
15
15
16
```js run
16
17
let str ="1 turkey costs 30€";
17
18
18
19
alert( str.match(/\d+(?=€)/) ); // 30, the number 1 is ignored, as it's not followed by €
19
20
```
20
21
21
-
Please note: the lookahead is merely a test, the contents of the parentheses `pattern:(?=...)` is not included in the result `match:30`.
22
+
يرجى ملاحظة: lookahead هو مجرد اختبار ، ومحتويات "نمط قوسين: (؟ = ...)` غير مدرجة في النتيجة
23
+
`match:30`.
22
24
23
-
When we look for `pattern:X(?=Y)`, the regular expression engine finds `pattern:X` and then checks if there's `pattern:Y` immediately after it. If it's not so, then the potential match is skipped, and the search continues.
25
+
عندما نبحث عن "pattern: X (؟ = Y)` ، يعثر محرك التعبير العادي على "pattern: X" ثم يتحقق مما إذا كان هناك "pattern: Y" بعده مباشرة. إذا لم يكن الأمر كذلك ، يتم تخطي المطابقة المحتملة ، ويستمر البحث.
24
26
25
-
More complex tests are possible, e.g. `pattern:X(?=Y)(?=Z)`means:
27
+
من الممكن إجراء اختبارات أكثر تعقيدًا ، على سبيل المثال `النمط: X (؟ = Y) (؟ = Z)`يعني:
26
28
27
-
1.Find `pattern:X`.
28
-
2.Check if `pattern:Y` is immediately after `pattern:X` (skip if isn't).
29
-
3.Check if `pattern:Z` is also immediately after `pattern:X` (skip if isn't).
30
-
4.If both tests passed, then the `pattern:X` is a match, otherwise continue searching.
29
+
1.ابحث عن `النمط: X`.
30
+
2.تحقق مما إذا كان "النمط: Y" مباشرةً بعد "النمط: X" (يمكنك التخطي إذا لم يكن كذلك).
31
+
3.تحقق مما إذا كان "النمط: Z" هو أيضًا مباشرةً بعد "النمط: X" (يمكنك التخطي إذا لم يكن كذلك).
32
+
4.في حالة اجتياز كلا الاختبارين ، فإن "النمط: X" هو تطابق ، وإلا فتابع البحث.
31
33
32
-
In other words, such pattern means that we're looking for `pattern:X`followed by `pattern:Y`and`pattern:Z`at the same time.
34
+
بمعنى آخر ، يعني هذا النمط أننا نبحث عن `pattern:X`متبوعًا بـ`pattern:Y`و`pattern:Z`في نفس الوقت.
33
35
34
-
That's only possible if patterns `pattern:Y` and `pattern:Z` aren't mutually exclusive.
36
+
هذا ممكن فقط إذا كان النموذجان "pattern: Y" و "pattern: Z" لا يستبعد أحدهما الآخر.
37
+
38
+
على سبيل المثال ، `pattern: \ d + (؟ = \ s) (؟ =. * 30)` يبحث عن `pattern: \ d +` فقط إذا كان متبوعًا بمسافة ، ويوجد `30` في مكان ما بعده:
35
39
36
-
For example, `pattern:\d+(?=\s)(?=.*30)` looks for `pattern:\d+` only if it's followed by a space, and there's `30` somewhere after it:
37
40
38
41
```js run
39
42
let str ="1 turkey costs 30€";
40
43
41
44
alert( str.match(/\d+(?=\s)(?=.*30)/) ); // 1
42
45
```
43
46
44
-
In our string that exactly matches the number`1`.
47
+
في سلسلتنا التي تتطابق تمامًا مع الرقم`1`.
45
48
46
49
## Negative lookahead
47
50
48
-
Let's say that we want a quantity instead, not a price from the same string. That's a number `pattern:\d+`, NOT followed by `subject:€`.
51
+
لنفترض أننا نريد كمية بدلاً من ذلك ، وليس سعرًا من نفس السلسلة. هذا رقم `نمط: \ d +` ، وليس متبوعًا بـ `الموضوع: €`.
52
+
53
+
لذلك ، يمكن تطبيق lookahead سلبي.
49
54
50
-
For that, a negative lookahead can be applied.
55
+
الصيغة هي: `pattern: X (؟! Y)` ، وتعني "search` pattern: X` ، ولكن فقط إذا لم يتبعها "pattern: Y`".
51
56
52
-
The syntax is: `pattern:X(?!Y)`, it means "search `pattern:X`, but only if not followed by `pattern:Y`".
53
57
54
58
```js run
55
59
let str ="2 turkeys cost 60€";
@@ -59,15 +63,15 @@ alert( str.match(/\d+(?!€)/) ); // 2 (the price is skipped)
59
63
60
64
## Lookbehind
61
65
62
-
Lookahead allows to add a condition for "what follows".
66
+
يسمح Lookahead بإضافة شرط لـ "ما يلي".
63
67
64
-
Lookbehind is similar, but it looks behind. That is, it allows to match a pattern only if there's something before it.
68
+
Lookbehind مشابه ، لكنه يبدو في الخلف. أي أنه يسمح بمطابقة النمط فقط إذا كان هناك شيء قبله.
65
69
66
-
The syntax is:
67
-
-Positive lookbehind: `pattern:(?<=Y)X`, matches `pattern:X`, but only if there's `pattern:Y`before it.
68
-
-Negative lookbehind: `pattern:(?<!Y)X`, matches `pattern:X`, but only if there's no `pattern:Y`before it.
70
+
الصيغة هي:
71
+
-نظرة إيجابية خلف: `pattern: (؟ <= Y) X` ، تطابق`pattern: X` ، ولكن فقط في حالة وجود "pattern:Y` قبلها.
72
+
-مظهر سلبي خلف: `pattern: (؟ <! Y) X` ، يطابق`pattern: X` ، ولكن فقط في حالة عدم وجود `pattern:Y`قبله.
69
73
70
-
For example, let's change the price to US dollars. The dollar sign is usually before the number, so to look for `$30`we'll use `pattern:(?<=\$)\d+` -- an amount preceded by `subject:$`:
74
+
على سبيل المثال ، دعنا نغير السعر إلى الدولار الأمريكي. عادةً ما تكون علامة الدولار قبل الرقم ، لذلك للبحث عن `$ 30`، سنستخدم``النمط: (؟ <= \ $) \ d +` - مبلغ يسبقه `الموضوع: $`:
alert( str.match(/(?<=\$)\d+/) ); // 30 (skipped the sole number)
77
81
```
78
82
79
-
And, if we need the quantity -- a number, not preceded by `subject:$`, then we can use a negative lookbehind `pattern:(?<!\$)\d+`:
83
+
وإذا احتجنا إلى الكمية - رقمًا ، لا يسبقه "الموضوع: $` ، فيمكننا استخدام النمط السلبي خلف ": (؟ <! \ $) \ d +`:
80
84
81
85
```js run
82
86
let str ="2 turkeys cost $60";
83
87
84
88
alert( str.match(/(?<!\$)\d+/) ); // 2 (skipped the price)
85
89
```
86
90
87
-
## Capturing groups
91
+
## التقاط المجموعات
88
92
89
-
Generally, the contents inside lookaround parentheses does not become a part of the result.
93
+
بشكل عام ، لا تصبح المحتويات الموجودة داخل الأقواس حول جزء من النتيجة.
90
94
91
-
E.g. in the pattern `pattern:\d+(?=€)`, the `pattern:€`sign doesn't get captured as a part of the match. That's natural: we look for a number `pattern:\d+`, while `pattern:(?=€)`is just a test that it should be followed by `subject:€`.
95
+
على سبيل المثال في النموذج `pattern: \ d + (؟ = €)` ، لا يتم التقاط علامة `pattern:€`كجزء من المباراة. هذا طبيعي: نحن نبحث عن رقم `نقش: \ d +` ، بينما `نقش: (؟ = €)`هو مجرد اختبار يجب أن يتبعه `الموضوع: €`.
92
96
93
-
But in some situations we might want to capture the lookaround expression as well, or a part of it. That's possible. Just wrap that part into additional parentheses.
97
+
ولكن في بعض المواقف ، قد نرغب في التقاط تعبير lookaround أيضًا ، أو جزء منه. أن من الممكن. ما عليك سوى لف هذا الجزء بأقواس إضافية.
94
98
95
-
In the example below the currency sign `pattern:(€|kr)` is captured, along with the amount:
99
+
في المثال أدناه ، تم تسجيل "نمط علامة العملة: (€ | kr)" ، بالإضافة إلى المبلغ:
96
100
97
101
```js run
98
102
let str ="1 turkey costs 30€";
@@ -101,7 +105,7 @@ let regexp = /\d+(?=(€|kr))/; // extra parentheses around €|kr
101
105
alert( str.match(regexp) ); // 30, €
102
106
```
103
107
104
-
And here's the same for lookbehind:
108
+
وإليك نفس الشيء بالنسبة إلى: lookbehind:
105
109
106
110
```js run
107
111
let str ="1 turkey costs $30";
@@ -110,21 +114,21 @@ let regexp = /(?<=(\$|£))\d+/;
110
114
alert( str.match(regexp) ); // 30, $
111
115
```
112
116
113
-
## Summary
117
+
## ملخص
114
118
115
-
Lookahead and lookbehind (commonly referred to as "lookaround") are useful when we'd like to match something depending on the context before/after it.
119
+
Lookahead و lookbehind (يشار إليهما عادةً باسم "lookaround") مفيدان عندما نرغب في مطابقة شيء ما اعتمادًا على السياق قبله / بعده.
116
120
117
-
For simple regexps we can do the similar thing manually. That is: match everything, in any context, and then filter by context in the loop.
121
+
بالنسبة إلى regexps البسيطة ، يمكننا القيام بنفس الشيء يدويًا. هذا هو: مطابقة كل شيء ، في أي سياق ، ثم التصفية حسب السياق في الحلقة.
118
122
119
-
Remember, `str.match` (without flag `pattern:g`) and`str.matchAll` (always) return matches as arrays with `index`property, so we know where exactly in the text it is, and can check the context.
123
+
تذكر أن `str.match` (بدون العلامة`pattern:g`) و`str.matchAll` (دائمًا) ترجع التطابقات كمصفوفات مع خاصية`index`، حتى نعرف مكانها بالضبط في النص ، ويمكننا التحقق من سياق الكلام.
0 commit comments