From 03ccff4b6c60d037b91c057e24fba75234a3212d Mon Sep 17 00:00:00 2001 From: Saiful Date: Thu, 21 May 2020 16:54:04 +0600 Subject: [PATCH] unicode flag translate --- .../03-regexp-unicode/article.md | 198 +++++++++--------- 1 file changed, 99 insertions(+), 99 deletions(-) diff --git a/9-regular-expressions/03-regexp-unicode/article.md b/9-regular-expressions/03-regexp-unicode/article.md index 82c00d7df..02c70d9fb 100644 --- a/9-regular-expressions/03-regexp-unicode/article.md +++ b/9-regular-expressions/03-regexp-unicode/article.md @@ -1,12 +1,12 @@ -# Unicode: flag "u" and class \p{...} +# ইউনিকোড: "u" ফ্ল্যাগ এবং \p{...} ক্লাস -JavaScript uses [Unicode encoding](https://en.wikipedia.org/wiki/Unicode) for strings. Most characters are encoding with 2 bytes, but that allows to represent at most 65536 characters. +স্ট্রিং এর জন্য জাভাস্ক্রিপ্ট [ইউনিকোড এনকোডিং](https://en.wikipedia.org/wiki/Unicode) ব্যবহার করে। বেশিরভাগ ক্যারাক্টার এনকোডিং হয় ২ বাইটে, এবং এর মাধ্যমে সর্বোচ্চ ৬৫৫৩৬ ক্যারাক্টারকে এনকোড করা যায়। -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. +এই সীমার মধ্যে সকল ক্যারাক্টারকে এনকোড করা সম্ভব না, এজন্য কিছু কিছু ক্যারাক্টার এনকোড হয় ৪ বাইটে, যেমন `𝒳` (ম্যাথমেটিক্যাল X) বা `😄` (হাসির ইমোজি) অথবা কিছু হায়ারোগ্লিপ চিহ্ন। -Here are the unicode values of some characters: +কিছু ক্যারাক্টারের ইউনিকোড ভ্যালু: -| Character | Unicode | Bytes count in unicode | +| ক্যারাক্টার | ইউনিকোড | ইউনিকোডে বাইটের মান | |------------|---------|--------| | a | `0x0061` | 2 | | ≈ | `0x2248` | 2 | @@ -14,108 +14,108 @@ Here are the unicode values of some characters: |𝒴| `0x1d4b4` | 4 | |😄| `0x1f604` | 4 | -So characters like `a` and `≈` occupy 2 bytes, while codes for `𝒳`, `𝒴` and `😄` are longer, they have 4 bytes. +তো কিছু ক্যারাক্টার যেমন `a` এবং `≈` ২ বাইট জায়গা দখল করে, যেখানে `𝒳`, `𝒴` এবং `😄` আরো বেশী জায়গা দখল করে, এরা ৪ বাইট জায়গা দখল করে। -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. +যখন জাভাস্ক্রীপ্ট তৈরি করা হয় তখন ইউনিকোড এনকোডিং সহজ ছিল: তখন ৪-বাইটের ক্যারাক্টার ছিলনা। তো, এজন্য কিছু স্ট্রিংয়ের ফাংশন সঠিকভাবে কাজ করেনা। -For instance, `length` thinks that here are two characters: +উদাহরণস্বরূপ, নিম্নোক্ত ক্যারাক্টারগুলোকে এর `length` এক এর বদলে দুই দেখায়: ```js run alert('😄'.length); // 2 alert('𝒳'.length); // 2 ``` -...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 ). +...কিন্তু আমরা দেখতে পাচ্ছি এখানে একটি ক্যারাক্টার, তাই না? এখানে `length` ৪ বাইট কে ২-বাইটের দুটি ক্যারাক্টার হিসেবে বিবেচনা করে। কিন্তু এটি ভুল, কেননা একে একসাথে একটি ক্যারাক্টার হিসেবে বিবেচনা করা উচিত (একে বলা হয় "surrogate pair"), এই পাঠে এই নিয়ে আরো বিস্তারিত জানতে পারবেন )। -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 . +ডিফল্টভাবে, রেগুলার এক্সপ্রেশন ৪-বাইটের "লং ক্যারাক্টার"-কে ২-বাইটের জোড়া হিসেবে বিবেচনা করে। এর ফলে স্ট্রিংয়ে আমরা উদ্ভট রেজাল্ট দেখতে পায়। এই নিয়ে আরো বিস্তারিত এই পাঠে জানতে পারবেন । -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. +রেগুলার এক্সপ্রেশনে এই ধরণের সমস্যা আমরা `pattern:u` ফ্ল্যাগের মাধ্যমে সমাধান করতে পারি। এই ফ্ল্যাগের সাহায্যে রেগুলার এক্সপ্রেশন ৪-বাইটের ক্যারাক্টারকে সঠিকভাবে ব্যবহার করতে পারে। এবং এর ফলে সার্চে ইউনিকোড প্রপার্টিগুলো সহজে পেয়ে যায়, আমরা পরবর্তীতে এই নিয়ে জানব। -## Unicode properties \p{...} +## ইউনিকোড প্রপার্টিজ \p{...} -```warn header="Not supported in Firefox and Edge" -Despite being a part of the standard since 2018, unicode proeprties are not supported in Firefox ([bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1361876)) and Edge ([bug](https://github.com/Microsoft/ChakraCore/issues/2969)). +```warn header="ফায়ারফক্স এবং এজে সমর্থিত নয়" +ইউনিকোড প্রপার্টি ২০১৮ স্ট্যান্ডার্ডের একটি অংশ, কিন্তু তারপরেও এটি ফায়ারফক্স ([bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1361876)) এবং এজে ([bug](https://github.com/Microsoft/ChakraCore/issues/2969)) সমর্থন করে না। -There's [XRegExp](http://xregexp.com) library that provides "extended" regular expressions with cross-browser support for unicode properties. +একটি [XRegExp](http://xregexp.com) লাইব্রেরী আছে যার মাধ্যমে রেগুলার এক্সপ্রেশনের "extended" ফিচারগুলো ক্রস ব্রাউজারের সাপোর্ট করানো যায়। ``` -Every character in Unicode has a lot of properties. They describe what "category" the character belongs to, contain miscellaneous information about it. +ইউনিকোডে প্রতিটি ক্যারাক্টারের একাধিক প্রপার্টি আছে। এটির সাহায্যে ক্যারাক্টারটি কোন ধরণের "ক্যাটাগরির" সাথে সম্পর্কিত তা জানা যায়, এছাড়াও এর বিশদ বর্ণনাও এতে থাকে। -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. +উদাহরণস্বরূপ, যদি কোন ক্যারাক্টারের `Letter` প্রপার্টি থাকে, এর মানে বুঝায় ক্যারাক্টারটি (যেকোন ভাষার) একটি বর্ণ এবং `Number` প্রপার্টি দ্বারা বুঝায় এটি আরবি, বাংলা অথবা চীনা ইত্যাদি ভাষার একটি অঙ্ক। -We can search for characters with a property, written as `pattern:\p{…}`. To use `pattern:\p{…}`, a regular expression must have flag `pattern:u`. +আমরা `pattern:\p{…}` প্রপার্টি দ্বারা ক্যারাক্টার সার্চ করতে পারি। `pattern:\p{…}` ব্যবহারের জন্য রেগুলার এক্সপ্রেশনে অবশ্যই `pattern:u` ফ্ল্যাগটি ব্যবহার করতে হবে। -For instance, `\p{Letter}` denotes a letter in any of language. We can also use `\p{L}`, as `L` is an alias of `Letter`. There are shorter aliases for almost every property. +উদাহরণস্বরূপ, `\p{Letter}` দ্বারা যেকোন ভাষার অক্ষরকে বুঝায়। আমরা `\p{L}`-ও ব্যবহার করতে পারি, এখানে `Letter` এর পরিবর্তে `L` ব্যবহার করা হয়। প্রায় প্রতিটি প্রপার্টির অ্যালিয়াস বা সংক্ষিপ্তনাম আছে। -In the example below three kinds of letters will be found: English, Georgean and Korean. +নিম্নোলিখিত উদাহরণে আমরা ৪ ধরণের অক্ষর খুঁজে পাব: ইংরেজি, জর্জিয়ান, কোরিয়ান এবং বাংলা। ```js run -let str = "A ბ ㄱ"; +let str = "A ბ ㄱ ক"; -alert( str.match(/\p{L}/gu) ); // A,ბ,ㄱ -alert( str.match(/\p{L}/g) ); // null (no matches, as there's no flag "u") +alert( str.match(/\p{L}/gu) ); // A,ბ,ㄱ, ক +alert( str.match(/\p{L}/g) ); // null (ফ্ল্যাগ "u" ছাড়া কোন মিল খুঁজে পাব না) ``` -Here's the main character categories and their subcategories: - -- Letter `L`: - - lowercase `Ll` - - modifier `Lm`, - - titlecase `Lt`, - - uppercase `Lu`, - - other `Lo`. -- Number `N`: - - decimal digit `Nd`, - - letter number `Nl`, - - other `No`. -- Punctuation `P`: - - connector `Pc`, - - dash `Pd`, - - initial quote `Pi`, - - final quote `Pf`, - - open `Ps`, - - close `Pe`, - - other `Po`. -- Mark `M` (accents etc): - - spacing combining `Mc`, - - enclosing `Me`, - - non-spacing `Mn`. -- Symbol `S`: - - currency `Sc`, - - modifier `Sk`, - - math `Sm`, - - other `So`. -- Separator `Z`: - - line `Zl`, - - paragraph `Zp`, - - space `Zs`. -- Other `C`: - - control `Cc`, - - format `Cf`, - - not assigned `Cn`, - -- private use `Co`, - - surrogate `Cs`. - - -So, e.g. if we need letters in lower case, we can write `pattern:\p{Ll}`, punctuation signs: `pattern:\p{P}` and so on. - -There are also other derived categories, like: -- `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`). -- `Hex_Digit` includes hexadecimal digits: `0-9`, `a-f`. -- ...And so on. - -Unicode supports many different properties, their full list would require a lot of space, so here are the references: - -- List all properties by a character: . -- List all characters by a property: . -- Short aliases for properties: . -- A full base of Unicode characters in text format, with all properties, is here: . - -### Example: hexadecimal numbers - -For instance, let's look for hexadecimal numbers, written as `xFF`, where `F` is a hex digit (0..1 or A..F). - -A hex digit can be denoted as `pattern:\p{Hex_Digit}`: +নিচে ক্যারাক্টারের ক্যাটাগরি এবং সাবক্যাটাগরি দেয়া হল: + +- লেটার `L`: + - ছোটহাতের `Ll` + - মোডিফায়ার `Lm`, + - টাইটেলকেস `Lt`, + - বড়হাতের `Lu`, + - অন্যান্য `Lo`। +- নাম্বার `N`: + - দশমিক সংখ্যা `Nd`, + - লেটার নাম্বার `Nl`, + - অন্যান্য `No`. +- যতিচিহ্ন `P`: + - কানেক্টর `Pc`, + - ড্যাশ `Pd`, + - প্রাথমিক উদ্ধৃতি চিহ্ন `Pi`, + - শেষ উদ্ধৃতি চিহ্ন `Pf`, + - খোলা বন্ধনী `Ps`, + - বদ্ধ বন্ধনী `Pe`, + - অন্যান্য `Po`. +- চিহ্ন `M` (যেমন অ্যাকসেন্টস): + - কোলন `Mc`, + - এনক্লোজিং `Me`, + - নন-স্পেসিং `Mn`. +- প্রতীক `S`: + - মুদ্রা `Sc`, + - মোডিফায়ার `Sk`, + - গাণিতিক `Sm`, + - অন্যান্য `So`. +- সেপারেটর `Z`: + - লাইন `Zl`, + - অনুচ্ছেদ `Zp`, + - স্পেস `Zs`. +- অন্যান্য `C`: + - কন্ট্রোল `Cc`, + - ফরম্যাট `Cf`, + - নির্দিষ্ট না `Cn`, + -- প্রাইভেট ব্যবহার `Co`, + - সারোগেট `Cs`। + + +তো, উদাহরণস্বরূপ যদি আমাদের ছোট হাতের অক্ষরগুলো লাগে আমরা এভাবে লিখতে পারি `pattern:\p{Ll}`, অনুরূপভাবে যতিচিহ্নের জন্য `pattern:\p{P}` ইত্যাদি। + +এছাড়াও আরো কিছু ডেরিভেটিভ ক্যাটাগরী আছে, যেমন: +- `Alphabetic` (`Alpha`) এর অন্তর্ভুক্ত হল অক্ষর `L`, এছাড়াও লেটার সংখ্যা `Nl` (যেমন Ⅻ - রোমান সংখ্যা পদ্ধতির ১২), এছাড়াও কিছু অন্যান্য চিহ্ন `Other_Alphabetic` (`OAlpha`). +- `Hex_Digit` হেক্সাডেসিমেল সংখ্যাসমূহ অন্তরভুক্ত: `0-9`, `a-f`. +- ...ইত্যাদি। + +ইউনিকোড বিভিন্ন বৈশিষ্ট্য সাপোর্ট করে, এদের বিস্তারিত এখানে দেয়া সম্ভব না, এ সম্পর্কে বিস্তারিত এখানে পাবেন: + +- ক্যারাক্টারের প্রপার্টিসমূহ: । +- প্রপার্টির ক্যারাক্টারসমূহ: । +- প্রপার্টিসমূহের সংক্ষিপ্তরূপ: । +- সম্পূর্ণ বেসসহ প্রপার্টি ও ক্যারাক্টারসমূহের টেক্সট ফরম্যাট: । + +### উদাহরণ: হেক্সাডেসিমেল সংখ্যা + +উদাহরণস্বরূপ, হেক্সাডেসিমেল সংখ্যাটি লক্ষ্য করুন, যা লিখা হয় `xFF` এভাবে, এখানে `F` হল একটি হেক্সাডেসিমেল অঙ্ক (0-1 এবং A-F)। + +একটি হেক্স ডিজিট `pattern:\p{Hex_Digit}` দ্বারা চিহ্নিত করা যায়: ```js run let regexp = /x\p{Hex_Digit}\p{Hex_Digit}/u; @@ -123,28 +123,28 @@ let regexp = /x\p{Hex_Digit}\p{Hex_Digit}/u; alert("number: xAF".match(regexp)); // xAF ``` -### Example: Chinese hieroglyphs +### উদাহরণ: চাইনিজ বর্ণমালা -Let's look for Chinese hieroglyphs. +চলুন এবার চাইনিজ বর্ণমালা দেখি। -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)"). +ইউনিকোডের একটি প্রপার্টি আছে `Script` (একটি নির্দিষ্ট লিখন পদ্ধতি), এর বিভিন্ন মান থাকতে পারে যেমন: `Cyrillic`, `Greek`, `Arabic`, `Han` (চাইনিজ) ইত্যাদি, [সম্পূর্ণ তালিকা]("https://en.wikipedia.org/wiki/Script_(Unicode)")। -To look for characters in a given writing system we should use `pattern:Script=`, e.g. for Cyrillic letters: `pattern:\p{sc=Cyrillic}`, for Chinese hieroglyphs: `pattern:\p{sc=Han}`, and so on: +আমরা এভাবে `pattern:Script=` একটি নির্দিষ্ট ভাষার বর্ণমালা খুঁজতে পারি যেমন: সিরিলিক বর্ণমালার জন্য: `pattern:\p{sc=Cyrillic}`, চাইনিজ বর্ণমালার জন্য: `pattern:\p{sc=Han}`, ইত্যাদি: ```js run -let regexp = /\p{sc=Han}/gu; // returns Chinese hieroglyphs +let regexp = /\p{sc=Han}/gu; // চাইনিজ বর্ণমালা রিটার্ন করবে let str = `Hello Привет 你好 123_456`; alert( str.match(regexp) ); // 你,好 ``` -### Example: currency +### উদাহরণ: কারেন্সি -Characters that denote a currency, such as `$`, `€`, `¥`, have unicode property `pattern:\p{Currency_Symbol}`, the short alias: `pattern:\p{Sc}`. - -Let's use it to look for prices in the format "currency, followed by a digit": +কিছু ক্যারাক্টার আছে যা দ্বারা কারেন্সি প্রকাশ করে, যেমন: `$`, `€`, `¥`, এদের প্রকাশের জন্য ইউনিকোডে `pattern:\p{Currency_Symbol}` এই প্রপার্টিটি আছে, এর সংক্ষিপ্তরূপ: `pattern:\p{Sc}`। +চলুন আমরা কারেন্সি দ্বারা প্রকাশিত মানগুলো খুঁজে বের করি: + ```js run let regexp = /\p{Sc}\d/gu; @@ -153,15 +153,15 @@ let str = `Prices: $2, €1, ¥9`; alert( str.match(regexp) ); // $2,€1,¥9 ``` -Later, in the article we'll see how to look for numbers that contain many digits. +পরবর্তীতে, এই অনুচ্ছেদে আমরা দেখব কিভাবে দুই বা ততোধিক অঙ্ক দ্বারা গঠিত সংখ্যাগুলো খুঁজা যায়। -## Summary +## সারাংশ -Flag `pattern:u` enables the support of Unicode in regular expressions. +`pattern:u` ফ্ল্যাগটি রেগুলার এক্সপ্রেশনে ইউনিকোড খুঁজার সাপোর্ট এনাবল করে। -That means two things: +এর ফলে দুটি ব্যাপার ঘটে: -1. Characters of 4 bytes are handled correctly: as a single character, not two 2-byte characters. -2. Unicode properties can be used in the search: `\p{…}`. +1. ৪ বাইটের ক্যারাক্টারগুলো সঠিকভাবে হ্যান্ডল করা যায়: ২-বাইটের দুটি ক্যারাক্টার হিসেবে বিবেচনা না করে একটি ক্যারাক্টার হিসেবে বিবেচনা করা হয়। +2. সার্চে ইউনিকোড প্রপার্টি `\p{…}` ব্যবহার করা যায়। -With Unicode properties we can look for words in given languages, special characters (quotes, currencies) and so on. +ইউনিকোড প্রপার্টির মাধ্যমে আমরা নির্দিষ্ট ভাষা, বিশেষ ক্যারাক্টার (উদ্ধৃতি, কারেন্সি) ইত্যাদি খুঁজতে পারি।