Replace unicode with Unicode all over the book
This commit is contained in:
parent
e87f130fc1
commit
7c73f64a13
6 changed files with 23 additions and 23 deletions
|
@ -56,7 +56,7 @@ There are only 6 of them in JavaScript:
|
|||
: Enables "dotall" mode, that allows a dot `pattern:.` to match newline character `\n` (covered in the chapter <info:regexp-character-classes>).
|
||||
|
||||
`pattern:u`
|
||||
: Enables full unicode support. The flag enables correct processing of surrogate pairs. More about that in the chapter <info:regexp-unicode>.
|
||||
: Enables full Unicode support. The flag enables correct processing of surrogate pairs. More about that in the chapter <info:regexp-unicode>.
|
||||
|
||||
`pattern:y`
|
||||
: "Sticky" mode: searching at the exact position in the text (covered in the chapter <info:regexp-sticky>)
|
||||
|
|
|
@ -4,9 +4,9 @@ JavaScript uses [Unicode encoding](https://en.wikipedia.org/wiki/Unicode) for st
|
|||
|
||||
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.
|
||||
|
||||
Here are the unicode values of some characters:
|
||||
Here are the Unicode values of some characters:
|
||||
|
||||
| Character | Unicode | Bytes count in unicode |
|
||||
| Character | Unicode | Bytes count in Unicode |
|
||||
|------------|---------|--------|
|
||||
| a | `0x0061` | 2 |
|
||||
| ≈ | `0x2248` | 2 |
|
||||
|
@ -121,7 +121,7 @@ alert("number: xAF".match(regexp)); // xAF
|
|||
|
||||
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)).
|
||||
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)).
|
||||
|
||||
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:
|
||||
|
||||
|
@ -135,7 +135,7 @@ 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}`.
|
||||
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":
|
||||
|
||||
|
|
|
@ -57,16 +57,16 @@ For instance:
|
|||
|
||||
- **\d** -- is the same as `pattern:[0-9]`,
|
||||
- **\w** -- is the same as `pattern:[a-zA-Z0-9_]`,
|
||||
- **\s** -- is the same as `pattern:[\t\n\v\f\r ]`, plus few other rare unicode space characters.
|
||||
- **\s** -- is the same as `pattern:[\t\n\v\f\r ]`, plus few other rare Unicode space characters.
|
||||
```
|
||||
|
||||
### Example: multi-language \w
|
||||
|
||||
As the character class `pattern:\w` is a shorthand for `pattern:[a-zA-Z0-9_]`, it can't find Chinese hieroglyphs, Cyrillic letters, etc.
|
||||
|
||||
We can write a more universal pattern, that looks for wordly characters in any language. That's easy with unicode properties: `pattern:[\p{Alpha}\p{M}\p{Nd}\p{Pc}\p{Join_C}]`.
|
||||
We can write a more universal pattern, that looks for wordly characters in any language. That's easy with Unicode properties: `pattern:[\p{Alpha}\p{M}\p{Nd}\p{Pc}\p{Join_C}]`.
|
||||
|
||||
Let's decipher it. Similar to `pattern:\w`, we're making a set of our own that includes characters with following unicode properties:
|
||||
Let's decipher it. Similar to `pattern:\w`, we're making a set of our own that includes characters with following Unicode properties:
|
||||
|
||||
- `Alphabetic` (`Alpha`) - for letters,
|
||||
- `Mark` (`M`) - for accents,
|
||||
|
@ -85,7 +85,7 @@ let str = `Hi 你好 12`;
|
|||
alert( str.match(regexp) ); // H,i,你,好,1,2
|
||||
```
|
||||
|
||||
Of course, we can edit this pattern: add unicode properties or remove them. Unicode properties are covered in more details in the article <info:regexp-unicode>.
|
||||
Of course, we can edit this pattern: add Unicode properties or remove them. Unicode properties are covered in more details in the article <info:regexp-unicode>.
|
||||
|
||||
```warn header="Unicode properties aren't supported in Edge and Firefox"
|
||||
Unicode properties `pattern:p{…}` are not yet implemented in Edge and Firefox. If we really need them, we can use library [XRegExp](http://xregexp.com/).
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue