Skip to content

Commit c1dd854

Browse files
authored
Merge pull request #37 from AhmedIbrahimGalal/regexp-introduction
Patterns and flags
2 parents e690f5c + 60ea5aa commit c1dd854

File tree

1 file changed

+78
-78
lines changed
  • 9-regular-expressions/01-regexp-introduction

1 file changed

+78
-78
lines changed
Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,177 +1,177 @@
1-
# Patterns and flags
1+
# الأنماط والأعلام
22

3-
Regular expressions are patterns that provide a powerful way to search and replace in text.
3+
التعبيرات العادية هي أنماط توفر طريقة فعالة للبحث والاستبدال في النص.
44

5-
In JavaScript, they are available via the [RegExp](mdn:js/RegExp) object, as well as being integrated in methods of strings.
5+
في ال JavaScript, أنها تكون متوفرة عبر [RegExp](mdn:js/RegExp), وكذلك يمكن دمجها في طرق النصوص.
66

7-
## Regular Expressions
7+
## التعبيرات المنتظمة
88

9-
A regular expression (also "regexp", or just "reg") consists of a *pattern* and optional *flags*.
9+
النعبير المنتظم (ويمكن أختصاره الي "regexp", او "reg") يتكون من *نمط* و*أعلام* أختيارية.
1010

11-
There are two syntaxes that can be used to create a regular expression object.
11+
هناك نوعان من بناء الجملة يمكن استخدامهما لإنشاء كائن تعبير منتظم.
1212

13-
The "long" syntax:
13+
طريقة الكتابة "الطويلة":
1414

1515
```js
16-
regexp = new RegExp("pattern", "flags");
16+
regexp = new RegExp("نمط", "أعلام");
1717
```
1818

19-
And the "short" one, using slashes `"/"`:
19+
وطريقة الكتابة "القصيرة", بأستخدام "/":
2020

2121
```js
22-
regexp = /pattern/; // no flags
23-
regexp = /pattern/gmi; // with flags g,m and i (to be covered soon)
22+
regexp = /نمط/; // بدون أعلام
23+
regexp = /نمط/gmi; // أستخدام الاعلام g,m والعلم i (سيتم تغطيته فيما بعد)
2424
```
2525

26-
Slashes `pattern:/.../` tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings.
26+
هذا الرمز "/.../" يعرف ال JavaScript أننا سننشئ تعبير منتظم. وهي كذلك مثل علامات التنصيص في النصوص.
2727

28-
In both cases `regexp` becomes an instance of the built-in `RegExp` class.
28+
في الحالتين يصبح ال `regexp` نموذج مبني من ال `RegExp`.
2929

30-
The main difference between these two syntaxes is that pattern using slashes `/.../` does not allow for expressions to be inserted (like string template literals with `${...}`). They are fully static.
30+
الأختلاف الرئيسي بين الطريقتين أن النمط بأستخدام ال `/.../` لا يسمح لك بكتابة تعبيرات بداخله (مثل التعبير `${...}`). في هذه الحاله تكون الجمله ثابتة.
3131

32-
Slashes are used when we know the regular expression at the code writing time -- and that's the most common situation. While `new RegExp`, is more often used when we need to create a regexp "on the fly" from a dynamically generated string. For instance:
32+
يتم استخدام الشرطة المائلة عندما نعرف التعبير العادي في وقت كتابة الكود -- وهذا هو الوضع الأكثر شيوعًا. بينما ال `new RegExp`, يتم استخدامه غالبًا عندما نحتاج إلى إنشاء regexp "على السريع" من سلسلة تم إنشاؤها ديناميكيًا. علي سبيل المثال:
3333

3434
```js
35-
let tag = prompt("What tag do you want to find?", "h2");
35+
let tag = prompt("ما العلامة التي تريد العثور عليها؟", "h2");
3636

37-
let regexp = new RegExp(`<${tag}>`); // same as /<h2>/ if answered "h2" in the prompt above
37+
let regexp = new RegExp(`<${tag}>`); // مثل /<h2>/ أذا أُجيب "h2" في prompt أعلاه
3838
```
3939

40-
## Flags
40+
## الأعلام
4141

42-
Regular expressions may have flags that affect the search.
42+
التعبيرات المنتظمة ربما تحتوي علي أعلام تؤثر في البحث.
4343

44-
There are only 6 of them in JavaScript:
44+
هناك فقط 6 منهم في ال JavaScript:
4545

4646
`pattern:i`
47-
: With this flag the search is case-insensitive: no difference between `A` and `a` (see the example below).
47+
: عند أستخدام هذا العلم. البحث لا يهتم بحالة الاحرف. فلا أختلاف بين ال `A` وال `a` (أنظر للمثال بالأسفل).
4848

4949
`pattern:g`
50-
: With this flag the search looks for all matches, without it -- only the first match is returned.
50+
: باستخدام هذه العلامة ، يبحث البحث عن جميع التطابقات ، بدونها -- يتم إرجاع المطابقة الأولى فقط
5151

5252
`pattern:m`
53-
: Multiline mode (covered in the chapter <info:regexp-multiline-mode>).
53+
: وضع متعدد الاسطر (تم تغطيتها في الفصل <info:regexp-multiline-mode>).
5454

5555
`pattern:s`
56-
: Enables "dotall" mode, that allows a dot `pattern:.` to match newline character `\n` (covered in the chapter <info:regexp-character-classes>).
56+
: يمكننا وضع ال "dotall", يسمح النقطة `pattern:.` لمطابقة السطر الجديد `\n` (تم تغطيته في الفصل <info:regexp-character-classes>).
5757

5858
`pattern:u`
59-
: Enables full unicode support. The flag enables correct processing of surrogate pairs. More about that in the chapter <info:regexp-unicode>.
59+
: تمكين دعم Unicode الكامل. يتيح العلم المعالجة الصحيحة للأزواج البديلة. المزيد عن ذلك في الفصل <info:regexp-unicode>.
6060

6161
`pattern:y`
62-
: "Sticky" mode: searching at the exact position in the text (covered in the chapter <info:regexp-sticky>)
62+
: وضع ال "Sticky": يبحث عن الموضع في النص (تم تغطيتها في الفصل <info:regexp-sticky>)
6363

64-
```smart header="Colors"
65-
From here on the color scheme is:
64+
```smart header="الألوان"
65+
من هنا في نظام الألوان:
6666
67-
- regexp -- `pattern:red`
68-
- string (where we search) -- `subject:blue`
69-
- result -- `match:green`
67+
- التعبير المنتظم -- `pattern:red`
68+
- النص (حيث نبحث) -- `subject:blue`
69+
- النتيجة -- `match:green`
7070
```
7171

72-
## Searching: str.match
72+
## البحث بأستخدام: str.match
7373

74-
As mentioned previously, regular expressions are integrated with string methods.
74+
كما ذكرنا سابقًا ، يتم دمج التعبيرات المنتظمة مع وظائف النص.
7575

76-
The method `str.match(regexp)` finds all matches of `regexp` in the string `str`.
76+
الوظيفة `str.match(regexp)` تجد كل ما يحتوي علي `regexp` في النص `str`.
7777

78-
It has 3 working modes:
78+
لديها 3 طرق عمل:
7979

80-
1. If the regular expression has flag `pattern:g`, it returns an array of all matches:
80+
1. أذا كان التعبير المنتظم يحتوي علي العلم `pattern:g`, تقوم بإرجاع مجموعة من كافة التطابقات:
8181
```js run
8282
let str = "We will, we will rock you";
8383

84-
alert( str.match(/we/gi) ); // We,we (an array of 2 substrings that match)
84+
alert( str.match(/we/gi) ); // We,we (مصفوفة من نصين متطابقين)
8585
```
86-
Please note that both `match:We` and `match:we` are found, because flag `pattern:i` makes the regular expression case-insensitive.
86+
من فضلك انتبه ان كلا من ال `match:We` وال `match:we` تم أيجادهم, لان العلم `pattern:i` يجعل التعبير المنتظم لا يهتم بحالة الاحرف.
8787

88-
2. If there's no such flag it returns only the first match in the form of an array, with the full match at index `0` and some additional details in properties:
88+
2. إذا لم يكن هناك مثل هذا العلم ، فإنه يُرجع المطابقة الأولى فقط في شكل مصفوفة ، مع المطابقة الكاملة في الفهرس `0` وبعض التفاصيل الإضافية في الخصائص:
8989
```js run
9090
let str = "We will, we will rock you";
9191
92-
let result = str.match(/we/i); // without flag g
92+
let result = str.match(/we/i); // بدون العلم g
9393
94-
alert( result[0] ); // We (1st match)
94+
alert( result[0] ); // We (المتطابقة الاولي)
9595
alert( result.length ); // 1
9696
97-
// Details:
98-
alert( result.index ); // 0 (position of the match)
99-
alert( result.input ); // We will, we will rock you (source string)
97+
// االتفاصيل:
98+
alert( result.index ); // 0 (موضع الجزء الذي تم البحث عنه)
99+
alert( result.input ); // We will, we will rock you (النص)
100100
```
101-
The array may have other indexes, besides `0` if a part of the regular expression is enclosed in parentheses. We'll cover that in the chapter <info:regexp-groups>.
101+
قد تحتوي المصفوفة على فهارس أخرى ، إلى جانب `0` إذا تم تضمين جزء من التعبير المنتظم بين قوسين. سنتناول ذلك في الفصل <info:regexp-groups>.
102102

103-
3. And, finally, if there are no matches, `null` is returned (doesn't matter if there's flag `pattern:g` or not).
103+
3. وأخيراُ, أذا كان هناك ولا متطابقة, فأن الناتج يكون `null` (لا يهم أذا كان يوجد العلم `pattern:g` او لا يوجد).
104104

105-
This a very important nuance. If there are no matches, we don't receive an empty array, but instead receive `null`. Forgetting about that may lead to errors, e.g.:
105+
هذا فارق بسيط مهم للغاية. إذا لم تكن هناك تطابقات ، فلن نتلقى مصفوفة فارغة ، ولكن بدلاً من ذلك نتلقى `null`. قد يؤدي نسيان ذلك إلى حدوث أخطاء ، على سبيل المثال:
106106

107107
```js run
108108
let matches = "JavaScript".match(/HTML/); // = null
109109
110-
if (!matches.length) { // Error: Cannot read property 'length' of null
111-
alert("Error in the line above");
110+
if (!matches.length) { // خطأ: لا يمكن قراءة الخاصية 'length' لدي null
111+
alert("خطأ في السطر الأعلي");
112112
}
113113
```
114114

115-
If we'd like the result to always be an array, we can write it this way:
115+
إذا أردنا أن تكون النتيجة دائمًا مصفوفة ، فيمكننا كتابتها بهذه الطريقة:
116116

117117
```js run
118118
let matches = "JavaScript".match(/HTML/)*!* || []*/!*;
119119
120120
if (!matches.length) {
121-
alert("No matches"); // now it works
121+
alert("لا متطابقات"); // الأن أنها تعمل
122122
}
123123
```
124124

125-
## Replacing: str.replace
125+
## الأستبدال: str.replace
126126

127-
The method `str.replace(regexp, replacement)` replaces matches found using `regexp` in string `str` with `replacement` (all matches if there's flag `pattern:g`, otherwise, only the first one).
127+
الوظيفة `str.replace(regexp, replacement)` تستبدل المتطابقات التي يتم أيجادها بأستخدام ال `regexp` في النص `str` مع كلمة `replacement` (كل المتطابقات يتم أستبدالها أذا كان هنالك العلم `pattern:g`, غير ذلك, يتم أستبدال المتطابقة الاولي فقط).
128128

129-
For instance:
129+
علي سبيل المثال:
130130

131131
```js run
132-
// no flag g
132+
// بدون العلم g
133133
alert( "We will, we will".replace(/we/i, "I") ); // I will, we will
134134
135-
// with flag g
135+
// مع أستخدام العلم g
136136
alert( "We will, we will".replace(/we/ig, "I") ); // I will, I will
137137
```
138138

139-
The second argument is the `replacement` string. We can use special character combinations in it to insert fragments of the match:
139+
العنصر الثاني يكون النص `replacement`. يمكننا استخدام تركيبات أحرف خاصة لإدراج أجزاء من المتطابقة:
140140

141-
| Symbols | Action in the replacement string |
141+
| الرموز | الاجراء في نص الأستبدال |
142142
|--------|--------|
143-
|`$&`|inserts the whole match|
144-
|<code>$&#096;</code>|inserts a part of the string before the match|
145-
|`$'`|inserts a part of the string after the match|
146-
|`$n`|if `n` is a 1-2 digit number, then it inserts the contents of n-th parentheses, more about it in the chapter <info:regexp-groups>|
147-
|`$<name>`|inserts the contents of the parentheses with the given `name`, more about it in the chapter <info:regexp-groups>|
148-
|`$$`|inserts character `$` |
143+
|`$&`|إدراج المتطابقة كاملة|
144+
|<code>$&#096;</code>|أدراج جزء من النص قبل المتطابقة|
145+
|`$'`|أدراج جزء من النص بعد المتطابقة|
146+
|`$n`|أذا `n` تكون رقم او رقمين, ثم يدرج محتويات الأقواس رقم n ، المزيد عنها في الفصل <info:regexp-groups>|
147+
|`$<name>`|يدرج محتويات الأقواس مع "الاسم" المحدد ، المزيد عنه في الفصل <info:regexp-groups>|
148+
|`$$`|أدراج حرف `$` |
149149

150-
An example with `pattern:$&`:
150+
مثال علي `pattern:$&`:
151151

152152
```js run
153-
alert( "I love HTML".replace(/HTML/, "$& and JavaScript") ); // I love HTML and JavaScript
153+
alert( "أنا أحب HTML".replace(/HTML/, "$& and JavaScript") ); // أنا أحب HTML and JavaScript
154154
```
155155

156-
## Testing: regexp.test
156+
## الأختبار: regexp.test
157157

158-
The method `regexp.test(str)` looks for at least one match, if found, returns `true`, otherwise `false`.
158+
الوظيفة `regexp.test(str)` يبحث عن تطابق واحد على الأقل, أذا وُجد, يكون الناتج او العائد `true`, غير ذلك `false`.
159159

160160
```js run
161-
let str = "I love JavaScript";
162-
let regexp = /LOVE/i;
161+
let str = "أنا أحب JavaScript";
162+
let regexp = /أحب/i;
163163
164164
alert( regexp.test(str) ); // true
165165
```
166166

167-
Later in this chapter we'll study more regular expressions, walk through more examples, and also meet other methods.
167+
لاحقًا في هذا الفصل ، سندرس تعبيرات أكثر انتظامًا ، وسنتعرف على المزيد من الأمثلة ، ونلتقي أيضًا بطرق أخرى.
168168

169-
Full information about the methods is given in the article <info:regexp-methods>.
169+
يتم توفير معلومات كاملة عن الأساليب في المقالة <info:regexp-methods>.
170170

171-
## Summary
171+
## الملخص
172172

173-
- A regular expression consists of a pattern and optional flags: `pattern:g`, `pattern:i`, `pattern:m`, `pattern:u`, `pattern:s`, `pattern:y`.
174-
- Without flags and special symbols (that we'll study later), the search by a regexp is the same as a substring search.
175-
- The method `str.match(regexp)` looks for matches: all of them if there's `pattern:g` flag, otherwise, only the first one.
176-
- The method `str.replace(regexp, replacement)` replaces matches found using `regexp` with `replacement`: all of them if there's `pattern:g` flag, otherwise only the first one.
177-
- The method `regexp.test(str)` returns `true` if there's at least one match, otherwise, it returns `false`.
173+
- يتكون التعبير المنتظم من نمط وأعلام اختيارية: `pattern:g`, `pattern:i`, `pattern:m`, `pattern:u`, `pattern:s`, `pattern:y`.
174+
- بدون أعلام او رموز خاصة (ذلك سوف ندرسه لاحقاً), البحث عن طريق regexp هو نفسه البحث عن سلسلة فرعية.
175+
- الوظيفة `str.match(regexp)` يبحث عن المتطابقات: كلها إن وجدت `pattern:g` علم, غير ذلك, فقط المتطابقة الاولي.
176+
- الوظيفة `str.replace(regexp, replacement)` تستبدل المتطابقات التي توجد بأستخدام `regexp` مع `replacement`: كلهم أن تم أيجادهم `pattern:g` علم, غير ذلك الاولي فقط.
177+
- الوظيفة `regexp.test(str)` تُعيد لنا `true` أذا كان هناك علي الاقل متطابقة واحدة, غير ذلك, تُعيد لنا `false`.

0 commit comments

Comments
 (0)