Skip to content

Commit 1e78023

Browse files
author
Ahmed
committed
translate to arabic
1 parent 7a4c4e9 commit 1e78023

1 file changed

Lines changed: 73 additions & 74 deletions

File tree

  • 9-regular-expressions/02-regexp-character-classes
Lines changed: 73 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
# Character classes
1+
# فئات الأحرف
22

3-
Consider a practical task -- we have a phone number like `"+7(903)-123-45-67"`, and we need to turn it into pure numbers: `79031234567`.
3+
أعتبر هذه المهمه تدريب -- لدينا رقم هاتف خلوي مثل `"+7(903)-123-45-67"`, ونريد تحويله من هذا الشكل الي أرقام فقط: `79031234567`.
44

5-
To do so, we can find and remove anything that's not a number. Character classes can help with that.
5+
للقيام بذلك ، يمكننا العثور على وإزالة أي شيء ليس رقمًا. يمكن أن تساعد فئات الأحرف في ذلك.
66

7-
A *character class* is a special notation that matches any symbol from a certain set.
7+
*فئات الاحرف* هو رمز خاص يطابق أي رمز من مجموعة معينة.
8+
89

9-
For the start, let's explore the "digit" class. It's written as `pattern:\d` and corresponds to "any single digit".
10-
11-
For instance, the let's find the first digit in the phone number:
10+
على سبيل المثال ، دعنا نعثر على الرقم الأول في رقم الهاتف:
1211

1312
```js run
1413
let str = "+7(903)-123-45-67";
@@ -18,39 +17,39 @@ let regexp = /\d/;
1817
alert( str.match(regexp) ); // 7
1918
```
2019

21-
Without the flag `pattern:g`, the regular expression only looks for the first match, that is the first digit `pattern:\d`.
20+
بدون العلم `pattern:g`, التعبير المنتظم ينظر فقط للمتطابقة الاولي, والذي يكون الرقم الاول `pattern:\d`.
2221

23-
Let's add the `pattern:g` flag to find all digits:
22+
لنقم بأضافة العلم `pattern:g` لأيجاد كل الارقام:
2423

2524
```js run
2625
let str = "+7(903)-123-45-67";
2726

2827
let regexp = /\d/g;
2928

30-
alert( str.match(regexp) ); // array of matches: 7,9,0,3,1,2,3,4,5,6,7
29+
alert( str.match(regexp) ); // مصفوفة من المتطابقات: 7,9,0,3,1,2,3,4,5,6,7
3130

32-
// let's make the digits-only phone number of them:
31+
// دعنا نحصل علي أرقام الهاتف الحلوي فقط
3332
alert( str.match(regexp).join('') ); // 79031234567
3433
```
3534

36-
That was a character class for digits. There are other character classes as well.
35+
وهذا كان فئة الحرف للأرقام. هناك فئات الاحرف الاخري كذلك.
3736

38-
Most used are:
37+
الأكثر أستخداماً:
3938

40-
`pattern:\d` ("d" is from "digit")
41-
: A digit: a character from `0` to `9`.
39+
`pattern:\d` ( الحرف "d" جاء من كلمة "digit" )
40+
: الرقم: يكون من `0` الي `9`.
4241

43-
`pattern:\s` ("s" is from "space")
44-
: A space symbol: includes spaces, tabs `\t`, newlines `\n` and few other rare characters, such as `\v`, `\f` and `\r`.
42+
`pattern:\s` ( الحرف "s" جاء من كلمة "space")
43+
: رمز المسافة: يحتوي علي مسافات او مسافات طويلة `\t` او أسطر جديده `\n` وعدد قليل من الاحرف النادرة الاخري, مثل `\v` و `\f` و `\r`.
4544

46-
`pattern:\w` ("w" is from "word")
47-
: A "wordly" character: either a letter of Latin alphabet or a digit or an underscore `_`. Non-Latin letters (like cyrillic or hindi) do not belong to `pattern:\w`.
45+
`pattern:\w` ( الحرف "w" جاء من كلمة "word")
46+
: الحرف هذا: إما حرف الأبجدية اللاتينية أو رقم أو تسطير أسفل السطر `_`. لا تنتمي الأحرف غير اللاتينية (مثل السيريلية أو الهندية) `pattern:\w`.
4847

49-
For instance, `pattern:\d\s\w` means a "digit" followed by a "space character" followed by a "wordly character", such as `match:1 a`.
48+
علي سبيل المثال, `pattern:\d\s\w` الرمز هذا يعني رقم يأتي بعده مسافة ثم يُتبع بحرف, مثل `match:1 a`.
5049

51-
**A regexp may contain both regular symbols and character classes.**
50+
**قد يحتوي التعبير المنتظم على كل من الرموز المنتظمة وفئات الاحرف.**
5251

53-
For instance, `pattern:CSS\d` matches a string `match:CSS` with a digit after it:
52+
علي سبيل المثال, `pattern:CSS\d` ينتج عنه نص `match:CSS` مع رقم يأتي بعده:
5453

5554
```js run
5655
let str = "Is there CSS4?";
@@ -59,145 +58,145 @@ let regexp = /CSS\d/
5958
alert( str.match(regexp) ); // CSS4
6059
```
6160

62-
Also we can use many character classes:
61+
أيضاً نستطيع أستخدام الكثير من فئات الأحرف:
6362

6463
```js run
6564
alert( "I love HTML5!".match(/\s\w\w\w\w\d/) ); // ' HTML5'
6665
```
6766

68-
The match (each regexp character class has the corresponding result character):
67+
الناتج (كل فئة حرف تقابلها الناتج الخاص بها):
6968

7069
![](love-html5-classes.svg)
7170

72-
## Inverse classes
71+
## الفئات العكسية
7372

74-
For every character class there exists an "inverse class", denoted with the same letter, but uppercased.
73+
يوجد لكل فئة أحرف "فئة معكوسة" ، يُشار إليها بالحرف نفسه ، ولكن بالأحرف الكبيرة.
7574

76-
The "inverse" means that it matches all other characters, for instance:
75+
يعني "معكوس" أنه يطابق جميع الأحرف الأخرى ، على سبيل المثال:
7776

7877
`pattern:\D`
79-
: Non-digit: any character except `pattern:\d`, for instance a letter.
78+
: غير رقمي: أي حرف ما عدا `pattern:\d`, علي سبيل المثال حرف نصي.
8079

8180
`pattern:\S`
82-
: Non-space: any character except `pattern:\s`, for instance a letter.
81+
: بدون مسافة: أي حرف ما عدا `pattern:\s`, علي سبيل المثال حرف نصي.
8382

8483
`pattern:\W`
85-
: Non-wordly character: anything but `pattern:\w`, e.g a non-latin letter or a space.
84+
: حرف غير نصي: أي شئ لكن `pattern:\w`, علي سبيل المثال حرف غير لاتيني او مسافة.
8685

87-
In the beginning of the chapter we saw how to make a number-only phone number from a string like `subject:+7(903)-123-45-67`: find all digits and join them.
86+
في بداية الفصل رأينا كيفية صنع رقم هاتف لرقم فقط من النص مثل`subject:+7(903)-123-45-67`: أيجاد كل الارقام وتم جمعهم.
8887

8988
```js run
9089
let str = "+7(903)-123-45-67";
9190

9291
alert( str.match(/\d/g).join('') ); // 79031234567
9392
```
9493

95-
An alternative, shorter way is to find non-digits `pattern:\D` and remove them from the string:
94+
طريقة بديلة ,أسهل هي العثور علي أي شئ غير الارقام `pattern:\D` من النص و حذفه من النص:
9695

9796
```js run
9897
let str = "+7(903)-123-45-67";
9998

10099
alert( str.replace(/\D/g, "") ); // 79031234567
101100
```
102101

103-
## A dot is "any character"
102+
## النقطة هي "أي حرف"
104103

105-
A dot `pattern:.` is a special character class that matches "any character except a newline".
104+
النقطة `pattern:.` تكون فئة حرف ويكون الناتج الخاص بها "أي حرف ما عدا سطر جديد".
106105

107-
For instance:
106+
علي سبيل المثال:
108107

109108
```js run
110109
alert( "Z".match(/./) ); // Z
111110
```
112111

113-
Or in the middle of a regexp:
112+
او يمكن أستخدامها في منتصف التعبير المنتظم:
114113

115114
```js run
116115
let regexp = /CS.4/;
117116

118117
alert( "CSS4".match(regexp) ); // CSS4
119118
alert( "CS-4".match(regexp) ); // CS-4
120-
alert( "CS 4".match(regexp) ); // CS 4 (space is also a character)
119+
alert( "CS 4".match(regexp) ); // CS 4 (المسافة تعتبر أيضاً حرف)
121120
```
122121

123-
Please note that a dot means "any character", but not the "absense of a character". There must be a character to match it:
122+
يرجى ملاحظة أن النقطة تعني "أي حرف" ، ولكن ليس "عدم وجود حرف". يجب أن يكون هناك حرف لمطابقته:
124123

125124
```js run
126-
alert( "CS4".match(/CS.4/) ); // null, no match because there's no character for the dot
125+
alert( "CS4".match(/CS.4/) ); // الناتج يكون null لان لا يجود أي حرف موضع النقطة.
127126
```
128127

129-
### Dot as literally any character with "s" flag
128+
### النقطة تكون أي حرف مثل العلم "s"
130129

131-
By default, a dot doesn't match the newline character `\n`.
130+
بشكل افتراضي ، لا تتطابق النقطة مع حرف السطر الجديد `\n`.
132131

133-
For instance, the regexp `pattern:A.B` matches `match:A`, and then `match:B` with any character between them, except a newline `\n`:
132+
علي سبيل المثال, التعبير المنتظم `pattern:A.B` يطابق `match:A`, ومن ثم `match:B` مع أي حرف بينهم, ما عدا السطر الجديد `\n`:
134133

135134
```js run
136-
alert( "A\nB".match(/A.B/) ); // null (no match)
135+
alert( "A\nB".match(/A.B/) ); // null (لا متطابقة)
137136
```
138137

139-
There are many situations when we'd like a dot to mean literally "any character", newline included.
138+
هناك العديد من المواقف عندما نرغب في أن تعني النقطة حرفياً "أي حرف" ، بما في ذلك السطر الجديد.
140139

141-
That's what flag `pattern:s` does. If a regexp has it, then a dot `pattern:.` matches literally any character:
140+
ماذا العلم `pattern:s` يفعل. اذا كان التعبير المنتظم يحتوي عليه, كذلك النقطة `pattern:.` ينتج عنها أي حرف:
142141

143142
```js run
144143
alert( "A\nB".match(/A.B/s) ); // A\nB (match!)
145144
```
146145

147-
````warn header="Not supported in Firefox, IE, Edge"
148-
Check <https://caniuse.com/#search=dotall> for the most recent state of support. At the time of writing it doesn't include Firefox, IE, Edge.
146+
````warn header="لا يدعم في Firefox و IE و Edge"
147+
أفحص هذا الموقع <https://caniuse.com/#search=dotall> للمزيد من المعلومات حول الدعم. وفي نفس وقت الكتابة لا تنتمي الي Firefox و IE و Edge.
149148
150-
Luckily, there's an alternative, that works everywhere. We can use a regexp like `pattern:[\s\S]` to match "any character".
149+
لحسن الحظ ، هناك بديل يعمل في كل مكان. نستطيع أستخدام تعبير منتظم مثل `pattern:[\s\S]` ليكون الناتج "أي حرف".
151150
152151
```js run
153-
alert( "A\nB".match(/A[\s\S]B/) ); // A\nB (match!)
152+
alert( "A\nB".match(/A[\s\S]B/) ); // A\nB (الناتج!)
154153
```
155154
156-
The pattern `pattern:[\s\S]` literally says: "a space character OR not a space character". In other words, "anything". We could use another pair of complementary classes, such as `pattern:[\d\D]`, that doesn't matter. Or even the `pattern:[^]` -- as it means match any character except nothing.
155+
الرمز `pattern:[\s\S]` يقول: "حرف مسافة او ليس حرف مسافة". وبعبارة أخرى ، "أي شيء". يمكننا أستخدام زوج أخر من الفئات التكميلية, مثل `pattern:[\d\D]`, لا يهم أو حتي `pattern:[^]` -- لأنه يعني مطابقة أي حرف باستثناء أي شيء.
157156
158-
Also we can use this trick if we want both kind of "dots" in the same pattern: the actual dot `pattern:.` behaving the regular way ("not including a newline"), and also a way to match "any character" with `pattern:[\s\S]` or alike.
157+
كما يمكننا استخدام هذه الخدعة إذا أردنا نوعي "النقاط" في نفس النمط: النقطة الفعلية "النقط" في نفس الرمز: النقطة الفعلية `pattern:.` يشتغل بالطريقة المنتظمة ("لا يحتوي علي سطر جديد"), وأيضاً طريقة أخري للحصول علي "أي حرف" مع الرمز `pattern:[\s\S]` او علي حد سواء.
159158
````
160159

161-
````warn header="Pay attention to spaces"
162-
Usually we pay little attention to spaces. For us strings `subject:1-5` and `subject:1 - 5` are nearly identical.
160+
````warn header="أنتبه للمسافات"
161+
عادة نولي القليل من الاهتمام للمساحات. للنصوص `subject:1-5` و `subject:1 - 5` متطابة تقريباً.
163162
164-
But if a regexp doesn't take spaces into account, it may fail to work.
163+
ولكن أذا كان التعبير المنتظم لا يحسب مسافات, قد تفشل في العمل.
165164
166-
Let's try to find digits separated by a hyphen:
165+
فلنحاول أيجاد الأرقام مفصوله بواصلة:
167166
168167
```js run
169-
alert( "1 - 5".match(/\d-\d/) ); // null, no match!
168+
alert( "1 - 5".match(/\d-\d/) ); // null, لا ناتج!
170169
```
171170
172-
Let's fix it adding spaces into the regexp `pattern:\d - \d`:
171+
دعنا نصلحه بإضافة مسافات إلى التعبير المنتظم `pattern:\d - \d`:
173172
174173
```js run
175-
alert( "1 - 5".match(/\d - \d/) ); // 1 - 5, now it works
174+
alert( "1 - 5".match(/\d - \d/) ); // 1 - 5, الان أنها تعمل
176175
// or we can use \s class:
177-
alert( "1 - 5".match(/\d\s-\s\d/) ); // 1 - 5, also works
176+
alert( "1 - 5".match(/\d\s-\s\d/) ); // 1 - 5, أيضاً تعمل
178177
```
179178
180-
**A space is a character. Equal in importance with any other character.**
179+
**المسافة تعتبر حرف. تساوي في الاهمية مع أي حرف أخر.**
181180
182-
We can't add or remove spaces from a regular expression and expect to work the same.
181+
لا يمكننا إضافة مسافات أو إزالتها من التعبير المنتظم ونتوقع أن تعمل بالطريقة نفسها.
183182
184-
In other words, in a regular expression all characters matter, spaces too.
183+
بمعني أخر, في التعبير المنتظم كل الحروف لها أهمية, والمسافات أيضاً.
185184
````
186185

187-
## Summary
186+
## الملخص
188187

189-
There exist following character classes:
188+
توجد فئات الأحرف التالية:
190189

191-
- `pattern:\d` -- digits.
192-
- `pattern:\D` -- non-digits.
193-
- `pattern:\s` -- space symbols, tabs, newlines.
194-
- `pattern:\S` -- all but `pattern:\s`.
195-
- `pattern:\w` -- Latin letters, digits, underscore `'_'`.
196-
- `pattern:\W` -- all but `pattern:\w`.
197-
- `pattern:.` -- any character if with the regexp `'s'` flag, otherwise any except a newline `\n`.
190+
- `pattern:\d` -- الارقام.
191+
- `pattern:\D` -- غير الارقام.
192+
- `pattern:\s` -- مسافات و مسافات طويلة وسطر جديد.
193+
- `pattern:\S` -- أي شئ ما عدا `pattern:\s`.
194+
- `pattern:\w` -- الحروف اللاتينية و الارقام و التطسير `'_'`.
195+
- `pattern:\W` -- الكل ما عدا `pattern:\w`.
196+
- `pattern:.` -- أي حرف أذا كان مع التعبير المنتظم العلم `'s'`, غير ذلك يكون أي شئ ما عدا السطر الجديد `\n`.
198197

199-
...But that's not all!
198+
...ولكن هذا ليس كل شيء!
200199

201-
Unicode encoding, used by JavaScript for strings, provides many properties for characters, like: which language the letter belongs to (if it's a letter) it is it a punctuation sign, etc.
200+
يوفر ترميز Unicode ، الذي تستخدمه JavaScript للسلاسل ، العديد من الخصائص للأحرف ، مثل: اللغة التي ينتمي إليها الحرف (إذا كان حرفًا) فهو علامة ترقيم ، إلخ.
202201

203-
We can search by these properties as well. That requires flag `pattern:u`, covered in the next article.
202+
يمكننا البحث بهذه الخصائص أيضًا. هذا يتطلب العلم `pattern:u`, تم تغطيته في المقال التالي.

0 commit comments

Comments
 (0)