Skip to content

Commit d08c788

Browse files
authored
Merge pull request #41 from javascript-tutorial/revert-40-master
Revert "translate Alternate OR| into arabic"
2 parents 29aec33 + c1e142c commit d08c788

9 files changed

Lines changed: 83 additions & 91 deletions

File tree

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
يمكن أن تكون الفكرة الأولى هي سرد اللغات التي يوجد بها "|" في الوسط.
2+
The first idea can be to list the languages with `|` in-between.
33

4-
لكن هذا لا يعمل بشكل صحيح:
4+
But that doesn't work right:
55

66
```js run
77
let regexp = /Java|JavaScript|PHP|C|C\+\+/g;
@@ -11,18 +11,18 @@ let str = "Java, JavaScript, PHP, C, C++";
1111
alert( str.match(regexp) ); // Java,Java,PHP,C,C
1212
```
1313

14-
يبحث محرك التعبير العادي عن البدائل واحدة تلو الأخرى. هذا هو: أولاً يتحقق مما إذا كان لدينا `match: Java` ، وإلا - يبحث عن` match: JavaScript` وما إلى ذلك.
14+
The regular expression engine looks for alternations one-by-one. That is: first it checks if we have `match:Java`, otherwise -- looks for `match:JavaScript` and so on.
1515

16-
ونتيجة لذلك ، لا يمكن العثور على `match: JavaScript` مطلقًا ، فقط لأنه يتم تحديد` match: Java` أولاً.
16+
As a result, `match:JavaScript` can never be found, just because `match:Java` is checked first.
1717

18-
نفس الشيء مع `match: C` و` match: C ++ `.
18+
The same with `match:C` and `match:C++`.
1919

20-
هناك حلان لهذه المشكلة:
20+
There are two solutions for that problem:
2121

22-
1. قم بتغيير الترتيب للتحقق من المطابقة الأطول أولاً: `pattern: JavaScript | Java | C \ + \ + | C | PHP`.
23-
2. دمج المتغيرات بنفس البداية: `pattern: Java (Script)؟ | C (\ + \ +)؟ | PHP`.
22+
1. Change the order to check the longer match first: `pattern:JavaScript|Java|C\+\+|C|PHP`.
23+
2. Merge variants with the same start: `pattern:Java(Script)?|C(\+\+)?|PHP`.
2424

25-
بشكل عملي:
25+
In action:
2626

2727
```js run
2828
let regexp = /Java(Script)?|C(\+\+)?|PHP/g;
@@ -31,4 +31,3 @@ let str = "Java, JavaScript, PHP, C, C++";
3131

3232
alert( str.match(regexp) ); // Java,JavaScript,PHP,C,C++
3333
```
34-
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
# البحث عن لغات البرمجة
1+
# Find programming languages
22

3-
هناك العديد من لغات البرمجة ، مثل Java و JavaScript و PHP و C و C ++.
3+
There are many programming languages, for instance Java, JavaScript, PHP, C, C++.
44

5-
أنشئ التعبير العادي الذي يعثر عليه في السلسلة `الموضوع: Java JavaScript PHP C ++ C`:
5+
Create a regexp that finds them in the string `subject:Java JavaScript PHP C++ C`:
66

77
```js
88
let regexp = /your regexp/g;
99

1010
alert("Java JavaScript PHP C++ C".match(regexp)); // Java JavaScript PHP C++ C
1111
```
12-

9-regular-expressions/13-regexp-alternation/02-find-matching-bbtags/solution.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

2-
علامة الفتح هي `النمط: \ [(b | url | الاقتباس) \]`.
2+
Opening tag is `pattern:\[(b|url|quote)\]`.
33

4-
ثم للعثور على كل شيء حتى علامة الإغلاق - دعنا نستخدم النمط `pattern:.` مع العلامة `pattern: s` لمطابقة أي حرف بما في ذلك السطر الجديد ثم إضافة مرجع خلفي إلى علامة الإغلاق.
4+
Then to find everything till the closing tag -- let's use the pattern `pattern:.*?` with flag `pattern:s` to match any character including the newline and then add a backreference to the closing tag.
55

6-
النمط الكامل: `pattern: \ [(b | url | الاقتباس) \]. *؟ \ [/ \ 1 \]`.
6+
The full pattern: `pattern:\[(b|url|quote)\].*?\[/\1\]`.
77

8-
بشكل عملي:
8+
In action:
99

1010
```js run
1111
let regexp = /\[(b|url|quote)\].*?\[\/\1\]/gs;
@@ -20,4 +20,4 @@ let str = `
2020
alert( str.match(regexp) ); // [b]hello![/b],[quote][url]http://google.com[/url][/quote]
2121
```
2222

23-
يرجى ملاحظة أنه بالإضافة إلى الهروب من `نمط: [` و `نقش:]` ، كان علينا أن نهرب من شرطة مائلة لنموذج علامة الإغلاق: [\ / \ 1] `، لأن الخط المائل يغلق النمط عادةً.
23+
Please note that besides escaping `pattern:[` and `pattern:]`, we had to escape a slash for the closing tag `pattern:[\/\1]`, because normally the slash closes the pattern.
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
# البحث عن أزواج bbtag
1+
# Find bbtag pairs
22

3-
تبدو علامة "bb-tag" مثل `[tag] ... [/ tag]` ، حيث `tag` هي واحدة من:` b` أو `url` أو` quote`.
3+
A "bb-tag" looks like `[tag]...[/tag]`, where `tag` is one of: `b`, `url` or `quote`.
44

5-
على سبيل المثال:
5+
For instance:
66
```
77
[b]text[/b]
88
[url]http://google.com[/url]
99
```
1010

11-
يمكن أن تتداخل علامات BB. ولكن لا يمكن إدراج علامة في نفسها ، على سبيل المثال:
11+
BB-tags can be nested. But a tag can't be nested into itself, for instance:
1212

1313
```
14-
بشكل طبيعي:
14+
Normal:
1515
[url] [b]http://google.com[/b] [/url]
1616
[quote] [b]text[/b] [/quote]
1717
18-
لا يمكن حدوثه:
18+
Can't happen:
1919
[b][b]text[/b][/b]
2020
```
2121

22-
يمكن أن تحتوي العلامات على فواصل أسطر ، وهذا أمر طبيعي:
22+
Tags can contain line breaks, that's normal:
2323

2424
```
2525
[quote]
2626
[b]text[/b]
2727
[/quote]
2828
```
2929

30-
قم بإنشاء التعبير العادي للعثور على جميع علامات BB مع محتوياتها.
31-
على سبيل المثال:
30+
Create a regexp to find all BB-tags with their contents.
3231

32+
For instance:
3333

3434
```js
3535
let regexp = /your regexp/flags;
@@ -38,12 +38,11 @@ let str = "..[url]http://google.com[/url]..";
3838
alert( str.match(regexp) ); // [url]http://google.com[/url]
3939
```
4040

41-
إذا كانت العلامات متداخلة ، فإننا نحتاج إلى العلامة الخارجية (إذا أردنا ، يمكننا متابعة البحث في محتواها):
41+
If tags are nested, then we need the outer tag (if we want we can continue the search in its content):
4242

4343
```js
4444
let regexp = /your regexp/flags;
4545

4646
let str = "..[url][b]http://google.com[/b][/url]..";
4747
alert( str.match(regexp) ); // [url][b]http://google.com[/b][/url]
4848
```
49-
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
الحل: `pattern: /" (\\. | [^ "\\]) *" / g`.
1+
The solution: `pattern:/"(\\.|[^"\\])*"/g`.
22

3-
خطوة بخطوة:
3+
Step by step:
44

5-
- أولاً نبحث عن "نمط" الاقتباس الافتتاحي: "
6-
- ثم إذا كان لدينا نمط `` الشرطة المائلة للخلف '': `` (يجب علينا تقنيًا مضاعفته في النمط ، لأنه شخصية خاصة ، لذلك فهي خط مائل عكسي في الواقع) ، فإن أي حرف يكون جيدًا بعده (نقطة ).
7-
- وإلا فإننا نأخذ أي حرف باستثناء الاقتباس (وهذا يعني نهاية السلسلة) وشرطة مائلة للخلف (لمنع الخطوط المائلة العكسية الوحيدة ، يتم استخدام الشرطة المائلة للخلف فقط مع بعض الرموز الأخرى بعدها): `النمط: [^" \\] `
8-
- ... وهلم جرا حتى آخر quote أو علامة افتباس .
5+
- First we look for an opening quote `pattern:"`
6+
- Then if we have a backslash `pattern:\\` (we technically have to double it in the pattern, because it is a special character, so that's a single backslash in fact), then any character is fine after it (a dot).
7+
- Otherwise we take any character except a quote (that would mean the end of the string) and a backslash (to prevent lonely backslashes, the backslash is only used with some other symbol after it): `pattern:[^"\\]`
8+
- ...And so on till the closing quote.
99

10-
بشكل:
10+
In action:
1111

1212
```js run
1313
let regexp = /"(\\.|[^"\\])*"/g;
1414
let str = ' .. "test me" .. "Say \\"Hello\\"!" .. "\\\\ \\"" .. ';
1515

1616
alert( str.match(regexp) ); // "test me","Say \"Hello\"!","\\ \""
1717
```
18-
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1-
# البحث عن سلاسل الاقتباس
1+
# Find quoted strings
22

3-
قم بإنشاء regexp للعثور على سلاسل في علامات اقتباس مزدوجة `الموضوع:" ... "`.
3+
Create a regexp to find strings in double quotes `subject:"..."`.
44

5-
يجب أن تدعم السلاسل الهروب ، بنفس الطريقة التي تدعمها سلاسل JavaScript. على سبيل المثال ، يمكن إدراج علامات الاقتباس كـ `subject: \" `سطر جديد مثل` subject: \ n` ، والشرطة نفسها كـ `subject: \\`.
5+
The strings should support escaping, the same way as JavaScript strings do. For instance, quotes can be inserted as `subject:\"` a newline as `subject:\n`, and the slash itself as `subject:\\`.
66

77
```js
88
let str = "Just like \"here\".";
99
```
1010

11-
يرجى ملاحظة ، على وجه الخصوص ، أن الاقتباس الهارب `موضوع: \" `لا ينهي سلسلة.
11+
Please note, in particular, that an escaped quote `subject:\"` does not end a string.
1212

13-
لذلك يجب علينا البحث من اقتباس واحد إلى الآخر تجاهل علامات الاقتباس الهاربة على الطريق.
13+
So we should search from one quote to the other ignoring escaped quotes on the way.
1414

15-
هذا هو الجزء الأساسي من المهمة ، وإلا سيكون تافها.
15+
That's the essential part of the task, otherwise it would be trivial.
1616

17-
أمثلة على السلاسل المراد مطابقتها:
17+
Examples of strings to match:
1818
```js
1919
.. *!*"test me"*/!* ..
2020
.. *!*"Say \"Hello\"!"*/!* ... (escaped quotes inside)
2121
.. *!*"\\"*/!* .. (double slash inside)
2222
.. *!*"\\ \""*/!* .. (double slash and an escaped quote inside)
2323
```
2424

25-
في جافا سكريبت ، نحتاج إلى مضاعفة الخطوط المائلة لتمريرها مباشرة في السلسلة ، مثل هذا:
25+
In JavaScript we need to double the slashes to pass them right into the string, like this:
2626

2727
```js run
2828
let str = ' .. "test me" .. "Say \\"Hello\\"!" .. "\\\\ \\"" .. ';
2929

3030
// the in-memory string
3131
alert(str); // .. "test me" .. "Say \"Hello\"!" .. "\\ \"" ..
3232
```
33-
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11

2-
بداية النمط واضحة: `pattern: <style`.
2+
The pattern start is obvious: `pattern:<style`.
33

4-
... ولكن بعد ذلك لا يمكننا ببساطة كتابة `pattern: <style. *؟>` ، لأن `match: <styler>` ستطابقها.
4+
...But then we can't simply write `pattern:<style.*?>`, because `match:<styler>` would match it.
55

6-
نحتاج إلى مسافة بعد `match: <style` ثم اختياريًا شيء آخر أو` `match:>` النهائي.
6+
We need either a space after `match:<style` and then optionally something else or the ending `match:>`.
77

8-
في لغة regexp: `pattern: <style (> | \ s. *؟>)`.
8+
In the regexp language: `pattern:<style(>|\s.*?>)`.
99

10-
بشكل عملي:
10+
In action:
1111

1212
```js run
1313
let regexp = /<style(>|\s.*?>)/g;
1414

1515
alert( '<style> <styler> <style test="...">'.match(regexp) ); // <style>, <style test="...">
1616
```
17-
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
# البحث عن العلامة الكاملة
1+
# Find the full tag
22

3-
اكتب regexp للعثور على العلامة `<style ...>`. يجب أن تتطابق مع العلامة الكاملة: قد لا تحتوي على سمات `<style>` أو تحتوي على العديد منها `<style type =" ... "id =" ... ">`.
3+
Write a regexp to find the tag `<style...>`. It should match the full tag: it may have no attributes `<style>` or have several of them `<style type="..." id="...">`.
44

5-
... لكن التعبير العادي يجب ألا يطابق `<styler>`!
5+
...But the regexp should not match `<styler>`!
66

7-
على سبيل المثال:
7+
For instance:
88

99
```js
1010
let regexp = /your regexp/g;
1111

1212
alert( '<style> <styler> <style test="...">'.match(regexp) ); // <style>, <style test="...">
1313
```
14-
Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# التناوب (أو) |
1+
# Alternation (OR) |
22

3-
البديل هو المصطلح في التعبير العادي وهو في الواقع "OR" بسيط.
3+
Alternation is the term in regular expression that is actually a simple "OR".
44

5-
في التعبير العادي ، يُشار إليه بحرف السطر العمودي `pattern: |`.
5+
In a regular expression it is denoted with a vertical line character `pattern:|`.
66

7-
على سبيل المثال ، نحتاج إلى العثور على لغات البرمجة: HTML أو PHP أو Java أو JavaScript.
7+
For instance, we need to find programming languages: HTML, PHP, Java or JavaScript.
88

9-
التعبير العادي المقابل: `pattern: html | php | java (script)؟`.
9+
The corresponding regexp: `pattern:html|php|java(script)?`.
1010

11-
مثال للاستخدام:
11+
A usage example:
1212

1313
```js run
1414
let regexp = /html|php|css|java(script)?/gi;
@@ -18,54 +18,53 @@ let str = "First HTML appeared, then CSS, then JavaScript";
1818
alert( str.match(regexp) ); // 'HTML', 'CSS', 'JavaScript'
1919
```
2020

21-
لقد رأينا بالفعل شيئًا مشابهًا - الأقواس المربعة. تسمح بالاختيار بين عدة أحرف ، على سبيل المثال `pattern: gr [ae] y` يطابق` match: gre` أو `match: grey`.
21+
We already saw a similar thing -- square brackets. They allow to choose between multiple characters, for instance `pattern:gr[ae]y` matches `match:gray` or `match:grey`.
2222

23-
تسمح الأقواس المربعة باستخدام الأحرف أو مجموعات الأحرف فقط. يسمح التناوب بأي تعبيرات. نمط regexp `: A | B | C` يعني أحد التعبيرات` A` أو `B` أو` C`.
23+
Square brackets allow only characters or character sets. Alternation allows any expressions. A regexp `pattern:A|B|C` means one of expressions `A`, `B` or `C`.
2424

25-
على سبيل المثال:
25+
For instance:
2626

27-
- `pattern: gr (a | e) y` يعني تمامًا مثل` pattern: gr [ae] y`.
28-
- `pattern: gra | ey` تعني` match: gra` أو `match: ey`.
27+
- `pattern:gr(a|e)y` means exactly the same as `pattern:gr[ae]y`.
28+
- `pattern:gra|ey` means `match:gra` or `match:ey`.
2929

30-
لتطبيق التناوب على جزء مختار من النمط ، يمكننا تضمينه بين قوسين:
31-
- `النمط: أحب HTML | CSS` يطابق` مطابقة: أنا أحب HTML` أو `تطابق: CSS`.
32-
- `pattern: I love (HTML | CSS)` يتطابق مع `match: I love HTML` أو` match: I love CSS`.
30+
To apply alternation to a chosen part of the pattern, we can enclose it in parentheses:
31+
- `pattern:I love HTML|CSS` matches `match:I love HTML` or `match:CSS`.
32+
- `pattern:I love (HTML|CSS)` matches `match:I love HTML` or `match:I love CSS`.
3333

34-
## مثال: regexp للوقت
34+
## Example: regexp for time
3535

36-
في المقالات السابقة ، كانت هناك مهمة لبناء regexp لوقت البحث في شكل `hh: mm` ، على سبيل المثال` 12: 00`. لكن `` النمط البسيط: \ d \ d: \ d \ d` غامض للغاية. يقبل `25: 99` كوقت (حيث تتطابق 99 ثانية مع النمط ، لكن ذلك الوقت غير صالح).
36+
In previous articles there was a task to build a regexp for searching time in the form `hh:mm`, for instance `12:00`. But a simple `pattern:\d\d:\d\d` is too vague. It accepts `25:99` as the time (as 99 seconds match the pattern, but that time is invalid).
3737

38-
كيف يمكننا صنع نمط أفضل؟
38+
How can we make a better pattern?
3939

40-
يمكننا استخدام مطابقة أكثر دقة. اولا الساعات:
40+
We can use more careful matching. First, the hours:
4141

42-
- إذا كان الرقم الأول هو "0" أو "1" ، فيمكن أن يكون الرقم التالي أي: `pattern: [01] \ d`.
43-
- وإلا ، إذا كان الرقم الأول هو "2" ، فيجب أن يكون الرقم التالي "pattern: [0-3]`.
44-
- (غير مسموح بالرقم الأول الآخر)
42+
- If the first digit is `0` or `1`, then the next digit can be any: `pattern:[01]\d`.
43+
- Otherwise, if the first digit is `2`, then the next must be `pattern:[0-3]`.
44+
- (no other first digit is allowed)
4545

46-
يمكننا كتابة كلا المتغيرين في regexp باستخدام البديل: `pattern: [01] \ d | 2 [0-3]`.
46+
We can write both variants in a regexp using alternation: `pattern:[01]\d|2[0-3]`.
4747

48-
بعد ذلك ، يجب أن تكون الدقائق من `00` إلى` 59`. في لغة التعبير العادي التي يمكن كتابتها كـ `pattern: [0-5] \ d`: الرقم الأول` 0-5` ، ثم أي رقم.
48+
Next, minutes must be from `00` to `59`. In the regular expression language that can be written as `pattern:[0-5]\d`: the first digit `0-5`, and then any digit.
4949

50-
إذا صقنا الدقائق والثواني معًا ، نحصل على النمط: `pattern: [01] \ d | 2 [0-3]: [0-5] \ d`.
50+
If we glue minutes and seconds together, we get the pattern: `pattern:[01]\d|2[0-3]:[0-5]\d`.
5151

52-
لقد انتهينا تقريبًا ، ولكن هناك مشكلة. يحدث "النمط البديل": | `الآن بين` النمط: [01] \ d` و "النمط: 2 [0-3]: [0-5] \ d`.
52+
We're almost done, but there's a problem. The alternation `pattern:|` now happens to be between `pattern:[01]\d` and `pattern:2[0-3]:[0-5]\d`.
5353

54-
أي: تمت إضافة الدقائق إلى البديل الثاني ، إليك صورة واضحة:
54+
That is: minutes are added to the second alternation variant, here's a clear picture:
5555

56-
``
57-
[01] \ d | 2 [0-3]: [0-5] \ د
58-
``
56+
```
57+
[01]\d | 2[0-3]:[0-5]\d
58+
```
5959

60-
يبحث هذا النمط عن `النمط: [01] \ d` أو` النمط: 2 [0-3]: [0-5] \ d`.
60+
That pattern looks for `pattern:[01]\d` or `pattern:2[0-3]:[0-5]\d`.
6161

62-
ولكن هذا خطأ ، يجب استخدام التناوب فقط في جزء "الساعات" من التعبير العادي ، للسماح بـ "pattern: [01] \ d` OR` pattern: 2 [0-3] `. دعنا نصحح ذلك عن طريق وضع "ساعات" بين قوسين: `النمط: ([01] \ d | 2 [0-3]): [0-5] \ d`.
62+
But that's wrong, the alternation should only be used in the "hours" part of the regular expression, to allow `pattern:[01]\d` OR `pattern:2[0-3]`. Let's correct that by enclosing "hours" into parentheses: `pattern:([01]\d|2[0-3]):[0-5]\d`.
6363

64-
الحل النهائي:
64+
The final solution:
6565

6666
```js run
6767
let regexp = /([01]\d|2[0-3]):[0-5]\d/g;
6868

6969
alert("00:00 10:10 23:59 25:99 1:2".match(regexp)); // 00:00,10:10,23:59
7070
```
71-

0 commit comments

Comments
 (0)