Skip to content

Commit 7192b0d

Browse files
authored
Merge pull request #140 from MohamedAymanNeo/escaping-special-characters
Escaping, special characters
2 parents 49d8ecb + fcd037b commit 7192b0d

1 file changed

Lines changed: 42 additions & 36 deletions

File tree

  • 9-regular-expressions/07-regexp-escaping
Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,105 @@
11

2-
# Escaping, special characters
2+
# التخطى, الرموز الخاصة
33

4-
As we've seen, a backslash `pattern:\` is used to denote character classes, e.g. `pattern:\d`. So it's a special character in regexps (just like in regular strings).
4+
كما رأينا, الشرطة المائلة للخلف `pattern:\` تستخدم للدلالة على فئات الرموز,على سبيل المثال `pattern:d\`. لذلك فهى رمز خاص فى المصطلحات العادية (تمام كما هو الحال فى النصوص العادية).
55

6-
There are other special characters as well, that have special meaning in a regexp. They are used to do more powerful searches. Here's a full list of them: `pattern:[ \ ^ $ . | ? * + ( )`.
6+
هناك رموز خاصة أخرى كذلك, التى لها معنى خاص فى المصطلحات العادية. يتم استخدامها لإجراء عمليات بحث أكثر قوة .
7+
فيما يلى قائمة كاملة بها: `pattern:[\ ^ $ . | ? * + ( )`.
78

8-
Don't try to remember the list -- soon we'll deal with each of them separately and you'll know them by heart automatically.
9+
لا تحاول تذكر القائمة -- قريبا سنتعامل مع كل منهم على حدة وستعرفهم عن ظهر قلب تلقائيًا
910

10-
## Escaping
11+
## التخطى
1112

12-
Let's say we want to find literally a dot. Not "any character", but just a dot.
13+
لنفترض أننا نريد إيجاد نقطة حرفيا. ليس "اى حرف"
14+
لكن مجرد نقطة.
1315

14-
To use a special character as a regular one, prepend it with a backslash: `pattern:\.`.
16+
لاستخدام رمز خاص كرمز عادى, ضع قبلها شرطة مائلة للخلف: `pattern:.\`.
1517

16-
That's also called "escaping a character".
18+
هذا يسمى أيضا "تخطى الرموز".
1719

18-
For example:
20+
فمثلا:
1921
```js run
20-
alert( "Chapter 5.1".match(/\d\.\d/) ); // 5.1 (match!)
21-
alert( "Chapter 511".match(/\d\.\d/) ); // null (looking for a real dot \.)
22+
alert( "الفصل 5.1".match(/\d\.\d/) ); // 5.1 (تطابق!)
23+
alert( "الفصل 511".match(/\d\.\d/) ); // null (البحث عن نقطة حقيقية \.)
2224
```
2325

24-
Parentheses are also special characters, so if we want them, we should use `pattern:\(`. The example below looks for a string `"g()"`:
26+
الأقواس أيضا رموز خاصة, لذلك إذا أردناهم, يجب استخدام `pattern:)\`. المثال أدناه يبحث عن `"()g"`:
2527

2628
```js run
2729
alert( "function g()".match(/g\(\)/) ); // "g()"
2830
```
2931

30-
If we're looking for a backslash `\`, it's a special character in both regular strings and regexps, so we should double it.
32+
إذا كنا نبحث عن الشرطة المائلة للخلف `\`,إنها رمز خاص فى كلتا النصوص العادية و المصطلحات العادية, لذلك يجب أن نضاعفها.
3133

3234
```js run
3335
alert( "1\\2".match(/\\/) ); // '\'
3436
```
3537

36-
## A slash
38+
## الشرطة المائلة
3739

38-
A slash symbol `'/'` is not a special character, but in JavaScript it is used to open and close the regexp: `pattern:/...pattern.../`, so we should escape it too.
40+
رمز الشرطة المائلة `'/'` ليس رمز خاص ولكن فى الجافا سكريبت يتم استخدامه لفتح وإغلاق المصطلح العام: `pattern:/...نمط.../`,لذلك يجب تخطيها أيضا.
3941

40-
Here's what a search for a slash `'/'` looks like:
42+
هذا ما يبدو عليه البحث عن الشرطة المائلة `'/'`:
4143

4244
```js run
4345
alert( "/".match(/\//) ); // '/'
4446
```
4547

46-
On the other hand, if we're not using `pattern:/.../`, but create a regexp using `new RegExp`, then we don't need to escape it:
48+
من ناحية أخرى
49+
إذا كنا لا نستخدم `pattern:/.../`, ولكن قمنا بإنشاء مصطلع عام باستخدام `new RegExp`, فلا داعى لتخطيها:
4750

4851
```js run
49-
alert( "/".match(new RegExp("/")) ); // finds /
52+
alert( "/".match(new RegExp("/")) ); // وجدت /
5053
```
5154

5255
## new RegExp
5356

54-
If we are creating a regular expression with `new RegExp`, then we don't have to escape `/`, but need to do some other escaping.
57+
إذا كنا بصدد إنشاء تعبير عادي باستخدام `new RegExp`, كذلك لا يمكن تخطى `/`,
58+
ولكن بحاجة الى استخدام تخطى أخر.
5559

56-
For instance, consider this:
60+
61+
فمثلا, ضع فى اعتبارك هذا:
5762

5863
```js run
5964
let regexp = new RegExp("\d\.\d");
6065

61-
alert( "Chapter 5.1".match(regexp) ); // null
66+
alert( "الفصل 5.1".match(regexp) ); // null
6267
```
6368

64-
The similar search in one of previous examples worked with `pattern:/\d\.\d/`, but `new RegExp("\d\.\d")` doesn't work, why?
69+
البحث المماثل في أحد الأمثلة السابقة يعمل مع `pattern:/\d\.\d/`, ولكن `new RegExp("\d\.\d")` لا يعمل , لماذا؟
70+
71+
السبب هوا أن الشرط المائلة "تستهلك" بواسطة النص, كما قد نتذكر, النصوص العادية لديها رموز خاصة بها, مثل `n\`, والشرطة المائلة للخلف تستخدم فى التخطى.
6572

66-
The reason is that backslashes are "consumed" by a string. As we may recall, regular strings have their own special characters, such as `\n`, and a backslash is used for escaping.
6773

68-
Here's how "\d\.\d" is preceived:
74+
إليك كيف ينظر الى "\d\.\d" :
6975

7076
```js run
7177
alert("\d\.\d"); // d.d
7278
```
7379

74-
String quotes "consume" backslashes and interpret them on their own, for instance:
80+
النصوص الاقتباسية "تستهلك" الشرط المائلة للخلف وتفسرهم لأنفسهم, فمثلا:
7581

76-
- `\n` -- becomes a newline character,
77-
- `\u1234` -- becomes the Unicode character with such code,
78-
- ...And when there's no special meaning: like `pattern:\d` or `\z`, then the backslash is simply removed.
82+
- `n\` -- يضع سطر جديد,
83+
- `u1234\` -- يصبح رمز اليونيكود بالرقم المحدد,
84+
- ... وعندما لا يكون هناك معنى خاص: مثل `pattern:d\` أو `z\`, عندئذ يتم إزالة الشرطة المائلة للخلف ببساطة.
7985

80-
So `new RegExp` gets a string without backslashes. That's why the search doesn't work!
86+
لذلك `new RegExp` تحصل على النصوص بلا شرط مائلة للخلف. لهذا السبب لا يعمل البحث
8187

82-
To fix it, we need to double backslashes, because string quotes turn `\\` into `\`:
88+
ولإصلاحها ، نحتاج إلى مضاعفة الشرط المائلة للخلف, لأن النصوص المقتبسة تحول `\\` الى `\`:
8389

8490
```js run
8591
*!*
8692
let regStr = "\\d\\.\\d";
8793
*/!*
88-
alert(regStr); // \d\.\d (correct now)
94+
alert(regStr); // \d\.\d (صحيحة)
8995

9096
let regexp = new RegExp(regStr);
9197

92-
alert( "Chapter 5.1".match(regexp) ); // 5.1
98+
alert( "الفصل 5.1".match(regexp) ); // 5.1
9399
```
94100

95-
## Summary
101+
## الملخص
96102

97-
- To search for special characters `pattern:[ \ ^ $ . | ? * + ( )` literally, we need to prepend them with a backslash `\` ("escape them").
98-
- We also need to escape `/` if we're inside `pattern:/.../` (but not inside `new RegExp`).
99-
- When passing a string `new RegExp`, we need to double backslashes `\\`, cause string quotes consume one of them.
103+
- للبحث عن الرموز الخاصة `pattern:[ \ ^ $ . | ? * + ( )` حرفيا, نحن بحاجة الى إرفاقهم بشرطة مائلة للخلف `\` "(تخطيهم)"
104+
- نحن بحاجة أيضا الى تخطى `/` إذا كانت داخل `pattern:/.../` (ولكن ليست بداخل `new RegExp`)
105+
- عندما نمرر نصا الى `new RegExp`, نحن بحاجة الى مضاعفة الشرط المائلة للخلف `\\`, لأن النصوص المقتبسة تستهلك واحدة منهم.

0 commit comments

Comments
 (0)