|
1 | | -# Patterns and flags |
| 1 | +# الأنماط والأعلام |
2 | 2 |
|
3 | | -Regular expressions are patterns that provide a powerful way to search and replace in text. |
| 3 | +التعبيرات العادية هي أنماط توفر طريقة فعالة للبحث والاستبدال في النص. |
4 | 4 |
|
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), وكذلك يمكن دمجها في طرق النصوص. |
6 | 6 |
|
7 | | -## Regular Expressions |
| 7 | +## التعبيرات المنتظمة |
8 | 8 |
|
9 | | -A regular expression (also "regexp", or just "reg") consists of a *pattern* and optional *flags*. |
| 9 | +النعبير المنتظم (ويمكن أختصاره الي "regexp", او "reg") يتكون من *نمط* و*أعلام* أختيارية. |
10 | 10 |
|
11 | | -There are two syntaxes that can be used to create a regular expression object. |
| 11 | +هناك نوعان من بناء الجملة يمكن استخدامهما لإنشاء كائن تعبير منتظم. |
12 | 12 |
|
13 | | -The "long" syntax: |
| 13 | +طريقة الكتابة "الطويلة": |
14 | 14 |
|
15 | 15 | ```js |
16 | | -regexp = new RegExp("pattern", "flags"); |
| 16 | +regexp = new RegExp("نمط", "أعلام"); |
17 | 17 | ``` |
18 | 18 |
|
19 | | -And the "short" one, using slashes `"/"`: |
| 19 | +وطريقة الكتابة "القصيرة", بأستخدام "/": |
20 | 20 |
|
21 | 21 | ```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 (سيتم تغطيته فيما بعد) |
24 | 24 | ``` |
25 | 25 |
|
26 | | -Slashes `pattern:/.../` tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings. |
| 26 | +هذا الرمز "/.../" يعرف ال JavaScript أننا سننشئ تعبير منتظم. وهي كذلك مثل علامات التنصيص في النصوص. |
27 | 27 |
|
28 | | -In both cases `regexp` becomes an instance of the built-in `RegExp` class. |
| 28 | +في الحالتين يصبح ال `regexp` نموذج مبني من ال `RegExp`. |
29 | 29 |
|
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 | +الأختلاف الرئيسي بين الطريقتين أن النمط بأستخدام ال `/.../` لا يسمح لك بكتابة تعبيرات بداخله (مثل التعبير `${...}`). في هذه الحاله تكون الجمله ثابتة. |
31 | 31 |
|
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 "على السريع" من سلسلة تم إنشاؤها ديناميكيًا. علي سبيل المثال: |
33 | 33 |
|
34 | 34 | ```js |
35 | | -let tag = prompt("What tag do you want to find?", "h2"); |
| 35 | +let tag = prompt("ما العلامة التي تريد العثور عليها؟", "h2"); |
36 | 36 |
|
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 أعلاه |
38 | 38 | ``` |
39 | 39 |
|
40 | | -## Flags |
| 40 | +## الأعلام |
41 | 41 |
|
42 | | -Regular expressions may have flags that affect the search. |
| 42 | +التعبيرات المنتظمة ربما تحتوي علي أعلام تؤثر في البحث. |
43 | 43 |
|
44 | | -There are only 6 of them in JavaScript: |
| 44 | +هناك فقط 6 منهم في ال JavaScript: |
45 | 45 |
|
46 | 46 | `pattern:i` |
47 | | -: With this flag the search is case-insensitive: no difference between `A` and `a` (see the example below). |
| 47 | +: عند أستخدام هذا العلم. البحث لا يهتم بحالة الاحرف. فلا أختلاف بين ال `A` وال `a` (أنظر للمثال بالأسفل). |
48 | 48 |
|
49 | 49 | `pattern:g` |
50 | | -: With this flag the search looks for all matches, without it -- only the first match is returned. |
| 50 | +: باستخدام هذه العلامة ، يبحث البحث عن جميع التطابقات ، بدونها -- يتم إرجاع المطابقة الأولى فقط |
51 | 51 |
|
52 | 52 | `pattern:m` |
53 | | -: Multiline mode (covered in the chapter <info:regexp-multiline-mode>). |
| 53 | +: وضع متعدد الاسطر (تم تغطيتها في الفصل <info:regexp-multiline-mode>). |
54 | 54 |
|
55 | 55 | `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>). |
57 | 57 |
|
58 | 58 | `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>. |
60 | 60 |
|
61 | 61 | `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>) |
63 | 63 |
|
64 | | -```smart header="Colors" |
65 | | -From here on the color scheme is: |
| 64 | +```smart header="الألوان" |
| 65 | +من هنا في نظام الألوان: |
66 | 66 |
|
67 | | -- regexp -- `pattern:red` |
68 | | -- string (where we search) -- `subject:blue` |
69 | | -- result -- `match:green` |
| 67 | +- التعبير المنتظم -- `pattern:red` |
| 68 | +- النص (حيث نبحث) -- `subject:blue` |
| 69 | +- النتيجة -- `match:green` |
70 | 70 | ``` |
71 | 71 |
|
72 | | -## Searching: str.match |
| 72 | +## البحث بأستخدام: str.match |
73 | 73 |
|
74 | | -As mentioned previously, regular expressions are integrated with string methods. |
| 74 | +كما ذكرنا سابقًا ، يتم دمج التعبيرات المنتظمة مع وظائف النص. |
75 | 75 |
|
76 | | -The method `str.match(regexp)` finds all matches of `regexp` in the string `str`. |
| 76 | +الوظيفة `str.match(regexp)` تجد كل ما يحتوي علي `regexp` في النص `str`. |
77 | 77 |
|
78 | | -It has 3 working modes: |
| 78 | +لديها 3 طرق عمل: |
79 | 79 |
|
80 | | -1. If the regular expression has flag `pattern:g`, it returns an array of all matches: |
| 80 | +1. أذا كان التعبير المنتظم يحتوي علي العلم `pattern:g`, تقوم بإرجاع مجموعة من كافة التطابقات: |
81 | 81 | ```js run |
82 | 82 | let str = "We will, we will rock you"; |
83 | 83 |
|
84 | | - alert( str.match(/we/gi) ); // We,we (an array of 2 substrings that match) |
| 84 | + alert( str.match(/we/gi) ); // We,we (مصفوفة من نصين متطابقين) |
85 | 85 | ``` |
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` يجعل التعبير المنتظم لا يهتم بحالة الاحرف. |
87 | 87 |
|
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` وبعض التفاصيل الإضافية في الخصائص: |
89 | 89 | ```js run |
90 | 90 | let str = "We will, we will rock you"; |
91 | 91 |
|
92 | | - let result = str.match(/we/i); // without flag g |
| 92 | + let result = str.match(/we/i); // بدون العلم g |
93 | 93 |
|
94 | | - alert( result[0] ); // We (1st match) |
| 94 | + alert( result[0] ); // We (المتطابقة الاولي) |
95 | 95 | alert( result.length ); // 1 |
96 | 96 |
|
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 (النص) |
100 | 100 | ``` |
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>. |
102 | 102 |
|
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` او لا يوجد). |
104 | 104 |
|
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`. قد يؤدي نسيان ذلك إلى حدوث أخطاء ، على سبيل المثال: |
106 | 106 |
|
107 | 107 | ```js run |
108 | 108 | let matches = "JavaScript".match(/HTML/); // = null |
109 | 109 |
|
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("خطأ في السطر الأعلي"); |
112 | 112 | } |
113 | 113 | ``` |
114 | 114 |
|
115 | | - If we'd like the result to always be an array, we can write it this way: |
| 115 | + إذا أردنا أن تكون النتيجة دائمًا مصفوفة ، فيمكننا كتابتها بهذه الطريقة: |
116 | 116 |
|
117 | 117 | ```js run |
118 | 118 | let matches = "JavaScript".match(/HTML/)*!* || []*/!*; |
119 | 119 |
|
120 | 120 | if (!matches.length) { |
121 | | - alert("No matches"); // now it works |
| 121 | + alert("لا متطابقات"); // الأن أنها تعمل |
122 | 122 | } |
123 | 123 | ``` |
124 | 124 |
|
125 | | -## Replacing: str.replace |
| 125 | +## الأستبدال: str.replace |
126 | 126 |
|
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`, غير ذلك, يتم أستبدال المتطابقة الاولي فقط). |
128 | 128 |
|
129 | | -For instance: |
| 129 | +علي سبيل المثال: |
130 | 130 |
|
131 | 131 | ```js run |
132 | | -// no flag g |
| 132 | +// بدون العلم g |
133 | 133 | alert( "We will, we will".replace(/we/i, "I") ); // I will, we will |
134 | 134 |
|
135 | | -// with flag g |
| 135 | +// مع أستخدام العلم g |
136 | 136 | alert( "We will, we will".replace(/we/ig, "I") ); // I will, I will |
137 | 137 | ``` |
138 | 138 |
|
139 | | -The second argument is the `replacement` string. We can use special character combinations in it to insert fragments of the match: |
| 139 | +العنصر الثاني يكون النص `replacement`. يمكننا استخدام تركيبات أحرف خاصة لإدراج أجزاء من المتطابقة: |
140 | 140 |
|
141 | | -| Symbols | Action in the replacement string | |
| 141 | +| الرموز | الاجراء في نص الأستبدال | |
142 | 142 | |--------|--------| |
143 | | -|`$&`|inserts the whole match| |
144 | | -|<code>$`</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>$`</code>|أدراج جزء من النص قبل المتطابقة| |
| 145 | +|`$'`|أدراج جزء من النص بعد المتطابقة| |
| 146 | +|`$n`|أذا `n` تكون رقم او رقمين, ثم يدرج محتويات الأقواس رقم n ، المزيد عنها في الفصل <info:regexp-groups>| |
| 147 | +|`$<name>`|يدرج محتويات الأقواس مع "الاسم" المحدد ، المزيد عنه في الفصل <info:regexp-groups>| |
| 148 | +|`$$`|أدراج حرف `$` | |
149 | 149 |
|
150 | | -An example with `pattern:$&`: |
| 150 | +مثال علي `pattern:$&`: |
151 | 151 |
|
152 | 152 | ```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 |
154 | 154 | ``` |
155 | 155 |
|
156 | | -## Testing: regexp.test |
| 156 | +## الأختبار: regexp.test |
157 | 157 |
|
158 | | -The method `regexp.test(str)` looks for at least one match, if found, returns `true`, otherwise `false`. |
| 158 | +الوظيفة `regexp.test(str)` يبحث عن تطابق واحد على الأقل, أذا وُجد, يكون الناتج او العائد `true`, غير ذلك `false`. |
159 | 159 |
|
160 | 160 | ```js run |
161 | | -let str = "I love JavaScript"; |
162 | | -let regexp = /LOVE/i; |
| 161 | +let str = "أنا أحب JavaScript"; |
| 162 | +let regexp = /أحب/i; |
163 | 163 |
|
164 | 164 | alert( regexp.test(str) ); // true |
165 | 165 | ``` |
166 | 166 |
|
167 | | -Later in this chapter we'll study more regular expressions, walk through more examples, and also meet other methods. |
| 167 | +لاحقًا في هذا الفصل ، سندرس تعبيرات أكثر انتظامًا ، وسنتعرف على المزيد من الأمثلة ، ونلتقي أيضًا بطرق أخرى. |
168 | 168 |
|
169 | | -Full information about the methods is given in the article <info:regexp-methods>. |
| 169 | +يتم توفير معلومات كاملة عن الأساليب في المقالة <info:regexp-methods>. |
170 | 170 |
|
171 | | -## Summary |
| 171 | +## الملخص |
172 | 172 |
|
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