Skip to content

Commit 5b03f80

Browse files
authored
Merge pull request #253 from EhsanShahbazii/master
Unicode: flag "u" and class \p{...}
2 parents 687a512 + 64b8932 commit 5b03f80

1 file changed

Lines changed: 93 additions & 93 deletions

File tree

Lines changed: 93 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,143 @@
1-
# Unicode: flag "u" and class \p{...}
1+
# یونیکد: پرچم "u" و کلاس {...}p\
22

3-
JavaScript uses [Unicode encoding](https://en.wikipedia.org/wiki/Unicode) for strings. Most characters are encoded with 2 bytes, but that allows to represent at most 65536 characters.
3+
جاوااسکریپت از [رمزگذاری یونیکد](https://en.wikipedia.org/wiki/Unicode) برای رشته ها استفاده می کند. اکثر کاراکترها با 2 بایت کدگذاری می شوند، اما این امکان را برای نمایش حداکثر 65536 کاراکتر فراهم می کند.
44

5-
That range is not big enough to encode all possible characters, that's why some rare characters are encoded with 4 bytes, for instance like `𝒳` (mathematical X) or `😄` (a smile), some hieroglyphs and so on.
5+
این محدوده به اندازه کافی بزرگ نیست تا همه کاراکترهای ممکن را رمزگذاری کند، به همین دلیل است که برخی از کاراکترهای کمیاب با 4 بایت کدگذاری می شوند، به عنوان مثال مانند `𝒳` (X ریاضی) یا `😄` (لبخند)، برخی از هیروگلیف ها و غیره.
66

7-
Here are the Unicode values of some characters:
7+
در اینجا مقادیر یونیکد برخی از کاراکترها آمده است:
88

9-
| Character | Unicode | Bytes count in Unicode |
9+
| کاراکتر | یونیکد | تعداد بایت یونیکد |
1010
|------------|---------|--------|
1111
| a | `0x0061` | 2 |
1212
|| `0x2248` | 2 |
1313
|𝒳| `0x1d4b3` | 4 |
1414
|𝒴| `0x1d4b4` | 4 |
1515
|😄| `0x1f604` | 4 |
1616

17-
So characters like `a` and `` occupy 2 bytes, while codes for `𝒳`, `𝒴` and `😄` are longer, they have 4 bytes.
17+
بنابراین کاراکتر هایی مانند `a` و `` 2 بایت را اشغال می کنند، در حالی که کدهای `𝒳`، `𝒴` و `😄` طولانی تر هستند و 4 بایت دارند.
1818

19-
Long time ago, when JavaScript language was created, Unicode encoding was simpler: there were no 4-byte characters. So, some language features still handle them incorrectly.
19+
مدت ها پیش، زمانی که زبان جاوااسکریپت ایجاد شد، رمزگذاری یونیکد ساده تر بود: هیچ کاراکتر 4 بایتی وجود نداشت. بنابراین برخی از ویژگی های زبان را به اشتباه مدیریت می کردند.
2020

21-
For instance, `length` thinks that here are two characters:
21+
به عنوان مثال، `length` فکر می کند که در اینجا دو کاراکتر وجود دارد:
2222

2323
```js run
2424
alert('😄'.length); // 2
2525
alert('𝒳'.length); // 2
2626
```
2727

28-
...But we can see that there's only one, right? The point is that `length` treats 4 bytes as two 2-byte characters. That's incorrect, because they must be considered only together (so-called "surrogate pair", you can read about them in the article <info:string>).
28+
...اما ما می توانیم ببینیم که فقط یک کاراکتر وجود دارد، درست است؟ نکته این است که `length` آن 4 بایت را به عنوان دو کاراکتر 2 بایتی در نظر می گیرد. این نادرست است، زیرا آنها باید فقط با هم در نظر گرفته شوند (به اصطلاح "surrogate pair"، می توانید در مورد آنها در مقاله <info:string> بخوانید).
2929

30-
By default, regular expressions also treat 4-byte "long characters" as a pair of 2-byte ones. And, as it happens with strings, that may lead to odd results. We'll see that a bit later, in the article <info:regexp-character-sets-and-ranges>.
30+
به‌ طور پیش‌ فرض، عبارات باقاعده نیز "کاراکتر های طولانی" 4 بایتی را به عنوان یک جفت 2 بایتی در نظر می‌ گیرند. همانطور که در مورد رشته ها اتفاق می افتد، ممکن است به نتایج عجیب و غریب منجر شود. این را کمی بعد، در مقاله <info:regexp-character-sets-and-ranges> خواهیم دید.
3131

32-
Unlike strings, regular expressions have flag `pattern:u` that fixes such problems. With such flag, a regexp handles 4-byte characters correctly. And also Unicode property search becomes available, we'll get to it next.
32+
برخلاف رشته‌ها، عبارات باقاعده دارای پرچم `pattern:u` هستند که چنین مشکلاتی را برطرف می‌ کند. با چنین پرچمی، یک regexp کاراکترهای 4 بایتی را به درستی مدیریت می کند. همچنین جستجوی ویژگی یونیکد در دسترس قرار می گیرد. در ادامه به آن خواهیم پرداخت.
3333

34-
## Unicode properties \p{...}
34+
## ویژگی های یونیکد {...}p\
3535

36-
Every character in Unicode has a lot of properties. They describe what "category" the character belongs to, contain miscellaneous information about it.
36+
هر کاراکتر در یونیکد دارای ویژگی های زیادی است. آنها توصیف می کنند که کاراکتر به چه "رده ای" تعلق دارد و حاوی اطلاعات متفرقه در مورد آن است.
3737

38-
For instance, if a character has `Letter` property, it means that the character belongs to an alphabet (of any language). And `Number` property means that it's a digit: maybe Arabic or Chinese, and so on.
38+
به عنوان مثال، اگر یک کاراکتر دارای ویژگی `Letter` باشد، به این معنی است که کاراکتر متعلق به الفبا (از هر زبان) است. ویژگی `Number` به این معنی است که آن کاراکتر یک رقم است: شاید عربی یا چینی و غیره.
3939

40-
We can search for characters with a property, written as `pattern:\p{…}`. To use `pattern:\p{…}`, a regular expression must have flag `pattern:u`.
40+
می‌ توانیم کاراکترهایی را با یک ویژگی جستجو کنیم که به صورت `{…}pattern:\p` نوشته شده است. برای استفاده از `{…}pattern:\p`، یک عبارت باقاعده باید دارای پرچم `pattern:u` باشد.
4141

42-
For instance, `\p{Letter}` denotes a letter in any language. We can also use `\p{L}`, as `L` is an alias of `Letter`. There are shorter aliases for almost every property.
42+
برای مثال، `{Letter}p\` یک حرف در هر زبانی را نشان می‌دهد. همچنین می‌ توانیم از `p{L}\` استفاده کنیم، زیرا `L` نام مستعار `Letter` است. تقریباً برای هر ویژگی نام مستعار کوتاه تری وجود دارد.
4343

44-
In the example below three kinds of letters will be found: English, Georgian and Korean.
44+
در مثال زیر سه نوع حرف وجود دارد: انگلیسی، گرجی و کره ای.
4545

4646
```js run
4747
let str = "A ბ ㄱ";
4848

4949
alert( str.match(/\p{L}/gu) ); // A,ბ,ㄱ
50-
alert( str.match(/\p{L}/g) ); // null (no matches, \p doesn't work without the flag "u")
50+
alert( str.match(/\p{L}/g) ); // null (کار نمی کند "u" بدون پرچم \p ،بدون انطباق)
5151
```
5252

53-
Here's the main character categories and their subcategories:
54-
55-
- Letter `L`:
56-
- lowercase `Ll`
57-
- modifier `Lm`,
58-
- titlecase `Lt`,
59-
- uppercase `Lu`,
60-
- other `Lo`.
61-
- Number `N`:
62-
- decimal digit `Nd`,
63-
- letter number `Nl`,
64-
- other `No`.
65-
- Punctuation `P`:
66-
- connector `Pc`,
67-
- dash `Pd`,
68-
- initial quote `Pi`,
69-
- final quote `Pf`,
70-
- open `Ps`,
71-
- close `Pe`,
72-
- other `Po`.
73-
- Mark `M` (accents etc):
74-
- spacing combining `Mc`,
75-
- enclosing `Me`,
76-
- non-spacing `Mn`.
77-
- Symbol `S`:
78-
- currency `Sc`,
79-
- modifier `Sk`,
80-
- math `Sm`,
81-
- other `So`.
82-
- Separator `Z`:
83-
- line `Zl`,
84-
- paragraph `Zp`,
85-
- space `Zs`.
86-
- Other `C`:
87-
- control `Cc`,
88-
- format `Cf`,
89-
- not assigned `Cn`,
90-
- private use `Co`,
91-
- surrogate `Cs`.
92-
93-
94-
So, e.g. if we need letters in lower case, we can write `pattern:\p{Ll}`, punctuation signs: `pattern:\p{P}` and so on.
95-
96-
There are also other derived categories, like:
97-
- `Alphabetic` (`Alpha`), includes Letters `L`, plus letter numbers `Nl` (e.g. Ⅻ - a character for the roman number 12), plus some other symbols `Other_Alphabetic` (`OAlpha`).
98-
- `Hex_Digit` includes hexadecimal digits: `0-9`, `a-f`.
99-
- ...And so on.
100-
101-
Unicode supports many different properties, their full list would require a lot of space, so here are the references:
102-
103-
- List all properties by a character: <https://unicode.org/cldr/utility/character.jsp>.
104-
- List all characters by a property: <https://unicode.org/cldr/utility/list-unicodeset.jsp>.
105-
- Short aliases for properties: <https://www.unicode.org/Public/UCD/latest/ucd/PropertyValueAliases.txt>.
106-
- A full base of Unicode characters in text format, with all properties, is here: <https://www.unicode.org/Public/UCD/latest/ucd/>.
107-
108-
### Example: hexadecimal numbers
109-
110-
For instance, let's look for hexadecimal numbers, written as `xFF`, where `F` is a hex digit (0..9 or A..F).
111-
112-
A hex digit can be denoted as `pattern:\p{Hex_Digit}`:
53+
در اینجا دسته بندی کاراکتر های اصلی و زیر شاخه های آنها آمده است:
54+
55+
- حرف `L`:
56+
- حروف کوچک `Ll`
57+
- اصلاح کننده `Lm`,
58+
- عنوان `Lt`,
59+
- حروف بزرگ `Lu`,
60+
- سایر `Lo`.
61+
- عدد `N`:
62+
- رقم اعشاری `Nd`,
63+
- شماره نامه `Nl`,
64+
- سایر `No`.
65+
- نقطه گذاری `P`:
66+
- اتصال دهنده `Pc`,
67+
- خط تیره `Pd`,
68+
- نقل قول اولیه `Pi`,
69+
- نقل قول نهایی `Pf`,
70+
- باز `Ps`,
71+
- بسته `Pe`,
72+
- سایر `Po`.
73+
- علامت `M` (لهجه ها و غیره):
74+
- ترکیب فاصله `Mc`,
75+
- محصور کردن `Me`,
76+
- بدون فاصله `Mn`.
77+
- نماد `S`:
78+
- واحد پول `Sc`,
79+
- اصلاح کننده `Sk`,
80+
- ریاضی `Sm`,
81+
- سایر `So`.
82+
- جداکننده `Z`:
83+
- خط `Zl`,
84+
- پاراگراف `Zp`,
85+
- فاصله `Zs`.
86+
- سایر `C`:
87+
- کنترل `Cc`,
88+
- فرمت `Cf`,
89+
- اختصاص داده نشده `Cn`,
90+
- استفاده خصوصی `Co`,
91+
- جانشین `Cs`.
92+
93+
94+
بنابراین، به عنوان مثال اگر به حروف کوچک نیاز داریم، می‌ توانیم `pattern:\p{Ll}`، علائم نگارشی: `pattern:\p{P}` و غیره را بنویسیم.
95+
96+
دسته های مشتق شده دیگری نیز وجود دارد، مانند:
97+
- `Alphabetic` (`Alpha`)، شامل حروف `L`، به اضافه اعداد حروف `Nl` (مثلاً Ⅻ - یک کاراکتر برای عدد رومی 12)، به‌علاوه برخی از نمادهای دیگر `Other_Alphabetic` (`OAlpha`).
98+
- `Hex_Digit` شامل اعداد هگزا دسیمال است. `0-9` `a-f`
99+
- ...و غیره.
100+
101+
یونیکد از بسیاری از ویژگی های مختلف پشتیبانی می کند، لیست کامل آنها به فضای زیادی نیاز دارد، بنابراین در اینجا منابع آمده است:
102+
103+
- لیست تمام ویژگی ها بر اساس یک کاراکتر: <https://unicode.org/cldr/utility/character.jsp>.
104+
- همه کاراکترها را بر اساس یک ویژگی: <https://unicode.org/cldr/utility/list-unicodeset.jsp>.
105+
- نام مستعار کوتاه برای خواص: <https://www.unicode.org/Public/UCD/latest/ucd/PropertyValueAliases.txt>.
106+
- یک پایه کامل از کاراکترهای یونیکد در قالب متن، با تمام خصوصیات، اینجا است: <https://www.unicode.org/Public/UCD/latest/ucd/>.
107+
108+
### مثال: اعداد هگزادسیمال
109+
110+
برای مثال، بیایید به دنبال اعداد هگزادسیمال بگردیم که به صورت `xFF` نوشته می‌ شوند، جایی که `F` یک رقم هگزاست (0..9 یا A..F).
111+
112+
یک رقم هگز را می توان به عنوان `pattern:\p{Hex_Digit}` نشان داد:
113113

114114
```js run
115115
let regexp = /x\p{Hex_Digit}\p{Hex_Digit}/u;
116116

117117
alert("number: xAF".match(regexp)); // xAF
118118
```
119119

120-
### Example: Chinese hieroglyphs
120+
### مثال: هیروگلیف چینی
121121

122-
Let's look for Chinese hieroglyphs.
122+
بیایید دنبال هیروگلیف چینی بگردیم.
123123

124-
There's a Unicode property `Script` (a writing system), that may have a value: `Cyrillic`, `Greek`, `Arabic`, `Han` (Chinese) and so on, [here's the full list](https://en.wikipedia.org/wiki/Script_(Unicode)).
124+
یک ویژگی یونیکد `Script` (یک سیستم نوشتاری) وجود دارد که ممکن است دارای مقدارهای روبرو باشد: `Cyrillic`، `Greek`، `Arabic`، `Han` (چینی) و غیره، [فهرست کامل در اینجا آمده است](https://en.wikipedia.org/wiki/Script_(Unicode)).
125125

126-
To look for characters in a given writing system we should use `pattern:Script=<value>`, e.g. for Cyrillic letters: `pattern:\p{sc=Cyrillic}`, for Chinese hieroglyphs: `pattern:\p{sc=Han}`, and so on:
126+
برای جستجوی کاراکترها در یک سیستم نوشتاری معین، باید از `<pattern:Script=<value` استفاده کنیم، به عنوان مثال. برای حروف سیریلیک: `pattern:\p{sc=Cyrillic}`، برای هیروگلیف چینی: `pattern:\p{sc=Han}` و غیره:
127127

128128
```js run
129-
let regexp = /\p{sc=Han}/gu; // returns Chinese hieroglyphs
129+
let regexp = /\p{sc=Han}/gu; // هیروگلیف های چینی را برمی گرداند
130130

131131
let str = `Hello Привет 你好 123_456`;
132132

133133
alert( str.match(regexp) ); // 你,好
134134
```
135135

136-
### Example: currency
136+
### مثال: ارز
137137

138-
Characters that denote a currency, such as `$`, ``, `¥`, have Unicode property `pattern:\p{Currency_Symbol}`, the short alias: `pattern:\p{Sc}`.
138+
کاراکتر ‌هایی که یک ارز را نشان می‌ دهند، مانند `$`، ``، `¥`، دارای ویژگی یونیکد `pattern:\p{Currency_Symbol}` هستند، نام مستعار کوتاه: `pattern:\p{Sc}`.
139139

140-
Let's use it to look for prices in the format "currency, followed by a digit":
140+
بیایید از آن برای جستجوی قیمت‌ها در قالب "ارز و به دنبال آن یک رقم" استفاده کنیم:
141141

142142
```js run
143143
let regexp = /\p{Sc}\d/gu;
@@ -147,15 +147,15 @@ let str = `Prices: $2, €1, ¥9`;
147147
alert( str.match(regexp) ); // $2,€1,¥9
148148
```
149149

150-
Later, in the article <info:regexp-quantifiers> we'll see how to look for numbers that contain many digits.
150+
بعداً، در مقاله <info:regexp-quantifiers> خواهیم دید که چگونه به دنبال اعدادی بگردیم که دارای ارقام زیادی هستند.
151151

152-
## Summary
152+
## خلاصه
153153

154-
Flag `pattern:u` enables the support of Unicode in regular expressions.
154+
پرچم `pattern:u` پشتیبانی از یونیکد را در عبارات منظم فعال می کند.
155155

156-
That means two things:
156+
یعنی دو چیز:
157157

158-
1. Characters of 4 bytes are handled correctly: as a single character, not two 2-byte characters.
159-
2. Unicode properties can be used in the search: `\p{…}`.
158+
1. کاراکترهای 4 بایتی به درستی مدیریت می شوند: به عنوان یک کاراکتر، نه دو کاراکتر 2 بایتی.
159+
2. از ویژگی های یونیکد می توان در جستجو استفاده کرد: `{…}p\`.
160160

161-
With Unicode properties we can look for words in given languages, special characters (quotes, currencies) and so on.
161+
با ویژگی‌های یونیکد می‌توانیم به دنبال کلمات در زبان‌های معین، کاراکترهای خاص (نقل‌ ها، ارزها) و غیره بگردیم.

0 commit comments

Comments
 (0)