From 5c0ea294ce0f16f4a07c3d3b794b10145ba920a5 Mon Sep 17 00:00:00 2001 From: Vaibhav Soni <49754221+vsoni101@users.noreply.github.com> Date: Thu, 3 Dec 2020 09:08:02 +0000 Subject: [PATCH 01/31] Update article.md In the section "String concatenation with binary +", the example of 2 + 2 + "1" was given, but there was no example for when the first operand is a string. --- 1-js/02-first-steps/08-operators/article.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/1-js/02-first-steps/08-operators/article.md b/1-js/02-first-steps/08-operators/article.md index 0aaaf512b..bb5f3f565 100644 --- a/1-js/02-first-steps/08-operators/article.md +++ b/1-js/02-first-steps/08-operators/article.md @@ -106,6 +106,11 @@ alert(2 + 2 + '1' ); // "41" and not "221" Here, operators work one after another. The first `+` sums two numbers, so it returns `4`, then the next `+` adds the string `1` to it, so it's like `4 + '1' = 41`. +```js run +alert('1' + 2 + 2); // "122" and not "14" +``` +Here, the first operand is a string, the compiller treats the other two operands as string too. The `2` gets concatinated to `'1'`, so it's like `'1' + 2 = "12"` and `"12" + 2 = "122"` + The binary `+` is the only operator that supports strings in such a way. Other arithmetic operators work only with numbers and always convert their operands to numbers. Here's the demo for subtraction and division: From 634e0668419351fdf06e70ead2c14a26a1a02ff9 Mon Sep 17 00:00:00 2001 From: Vaibhav Soni <49754221+vsoni101@users.noreply.github.com> Date: Thu, 3 Dec 2020 15:21:13 +0000 Subject: [PATCH 02/31] Update 1-js/02-first-steps/08-operators/article.md Co-authored-by: Vse Mozhe Buty --- 1-js/02-first-steps/08-operators/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/08-operators/article.md b/1-js/02-first-steps/08-operators/article.md index bb5f3f565..1beacd73f 100644 --- a/1-js/02-first-steps/08-operators/article.md +++ b/1-js/02-first-steps/08-operators/article.md @@ -109,7 +109,7 @@ Here, operators work one after another. The first `+` sums two numbers, so it re ```js run alert('1' + 2 + 2); // "122" and not "14" ``` -Here, the first operand is a string, the compiller treats the other two operands as string too. The `2` gets concatinated to `'1'`, so it's like `'1' + 2 = "12"` and `"12" + 2 = "122"` +Here, the first operand is a string, the compiler treats the other two operands as strings too. The `2` gets concatenated to `'1'`, so it's like `'1' + 2 = '12'` and `'12' + 2 = '122'`. The binary `+` is the only operator that supports strings in such a way. Other arithmetic operators work only with numbers and always convert their operands to numbers. From 3d86152fdd93fafdbd1c4863460651effdcec9dc Mon Sep 17 00:00:00 2001 From: Vaibhav Soni <49754221+vsoni101@users.noreply.github.com> Date: Thu, 3 Dec 2020 18:51:18 +0000 Subject: [PATCH 03/31] Update article.md Fix some quotes for consistency with above paragraphs --- 1-js/02-first-steps/08-operators/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/08-operators/article.md b/1-js/02-first-steps/08-operators/article.md index 1beacd73f..45b1f3426 100644 --- a/1-js/02-first-steps/08-operators/article.md +++ b/1-js/02-first-steps/08-operators/article.md @@ -109,7 +109,7 @@ Here, operators work one after another. The first `+` sums two numbers, so it re ```js run alert('1' + 2 + 2); // "122" and not "14" ``` -Here, the first operand is a string, the compiler treats the other two operands as strings too. The `2` gets concatenated to `'1'`, so it's like `'1' + 2 = '12'` and `'12' + 2 = '122'`. +Here, the first operand is a string, the compiler treats the other two operands as strings too. The `2` gets concatenated to `'1'`, so it's like `'1' + 2 = "12"` and `"12" + 2 = "122"`. The binary `+` is the only operator that supports strings in such a way. Other arithmetic operators work only with numbers and always convert their operands to numbers. From 76b42c3189dc5004b643cc77f3361a5b79bad966 Mon Sep 17 00:00:00 2001 From: Vse Mozhe Buty Date: Sun, 6 Dec 2020 22:08:01 +0200 Subject: [PATCH 04/31] Fix typo in 8.5 (Shadow DOM slots...) --- 8-web-components/5-slots-composition/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/8-web-components/5-slots-composition/article.md b/8-web-components/5-slots-composition/article.md index 38f356479..dbd28c5c1 100644 --- a/8-web-components/5-slots-composition/article.md +++ b/8-web-components/5-slots-composition/article.md @@ -446,7 +446,7 @@ Composition does not really move nodes, from JavaScript point of view the DOM is JavaScript can access slots using methods: - `slot.assignedNodes/Elements()` -- returns nodes/elements inside the `slot`. -- `node.assignedSlot` -- the reverse meethod, returns slot by a node. +- `node.assignedSlot` -- the reverse property, returns slot by a node. If we'd like to know what we're showing, we can track slot contents using: - `slotchange` event -- triggers the first time a slot is filled, and on any add/remove/replace operation of the slotted element, but not its children. The slot is `event.target`. From 79c951379640c651e07f2820be3084db7b8d12f9 Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Mon, 7 Dec 2020 17:20:04 +0300 Subject: [PATCH 05/31] closes #2358 --- 8-web-components/5-slots-composition/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/8-web-components/5-slots-composition/article.md b/8-web-components/5-slots-composition/article.md index dbd28c5c1..ada52a06a 100644 --- a/8-web-components/5-slots-composition/article.md +++ b/8-web-components/5-slots-composition/article.md @@ -408,7 +408,7 @@ customElements.define('custom-menu', class extends HTMLElement {
`; - // slottable is added/removed/replaced + // triggers when slot content changes *!* this.shadowRoot.firstElementChild.addEventListener('slotchange', e => { let slot = e.target; From adab443ae3061a4df01312896611af851e12eb1a Mon Sep 17 00:00:00 2001 From: Raphael <48417580+RapTho@users.noreply.github.com> Date: Mon, 7 Dec 2020 15:30:26 +0100 Subject: [PATCH 06/31] Fixed small typo 2-ui / 2-events / 5-dispatch-events In subchapter "Event constructor": first word use "Built-in" instead of "Build-in" --- 2-ui/2-events/05-dispatch-events/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2-ui/2-events/05-dispatch-events/article.md b/2-ui/2-events/05-dispatch-events/article.md index 930c51fcb..b38719f85 100644 --- a/2-ui/2-events/05-dispatch-events/article.md +++ b/2-ui/2-events/05-dispatch-events/article.md @@ -8,7 +8,7 @@ We can generate not only completely new events, that we invent for our own purpo ## Event constructor -Build-in event classes form a hierarchy, similar to DOM element classes. The root is the built-in [Event](http://www.w3.org/TR/dom/#event) class. +Built-in event classes form a hierarchy, similar to DOM element classes. The root is the built-in [Event](http://www.w3.org/TR/dom/#event) class. We can create `Event` objects like this: From 5be2c046a560e5e3504b18252c8da7695f6ac2a6 Mon Sep 17 00:00:00 2001 From: Vse Mozhe Buty Date: Mon, 7 Dec 2020 17:25:36 +0200 Subject: [PATCH 07/31] Fix typo in 9.1 (Patterns and flags) --- 9-regular-expressions/01-regexp-introduction/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/9-regular-expressions/01-regexp-introduction/article.md b/9-regular-expressions/01-regexp-introduction/article.md index a35d19a7b..1db3f5f9a 100644 --- a/9-regular-expressions/01-regexp-introduction/article.md +++ b/9-regular-expressions/01-regexp-introduction/article.md @@ -29,7 +29,7 @@ In both cases `regexp` becomes an instance of the built-in `RegExp` class. 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. -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: +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: ```js let tag = prompt("What tag do you want to find?", "h2"); From 2cf95d82baf732123cc8f377a27be3041dcb649a Mon Sep 17 00:00:00 2001 From: Vse Mozhe Buty Date: Mon, 7 Dec 2020 17:42:51 +0200 Subject: [PATCH 08/31] Fix some issues in 9.2 (Character classes) * Fix typos. * Update a support note. * Add a reference for a yet unexplained pattern. --- .../02-regexp-character-classes/article.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/9-regular-expressions/02-regexp-character-classes/article.md b/9-regular-expressions/02-regexp-character-classes/article.md index 93552a2b0..201c78a05 100644 --- a/9-regular-expressions/02-regexp-character-classes/article.md +++ b/9-regular-expressions/02-regexp-character-classes/article.md @@ -8,7 +8,7 @@ A *character class* is a special notation that matches any symbol from a certain For the start, let's explore the "digit" class. It's written as `pattern:\d` and corresponds to "any single digit". -For instance, the let's find the first digit in the phone number: +For instance, let's find the first digit in the phone number: ```js run let str = "+7(903)-123-45-67"; @@ -144,10 +144,10 @@ That's what flag `pattern:s` does. If a regexp has it, then a dot `pattern:.` ma alert( "A\nB".match(/A.B/s) ); // A\nB (match!) ``` -````warn header="Not supported in Firefox, IE, Edge" -Check for the most recent state of support. At the time of writing it doesn't include Firefox, IE, Edge. +````warn header="Not supported in IE" +The `pattern:s` flag is not supported in IE. -Luckily, there's an alternative, that works everywhere. We can use a regexp like `pattern:[\s\S]` to match "any character". +Luckily, there's an alternative, that works everywhere. We can use a regexp like `pattern:[\s\S]` to match "any character" (this pattern will be covered in the article ). ```js run alert( "A\nB".match(/A[\s\S]B/) ); // A\nB (match!) @@ -179,7 +179,7 @@ alert( "1 - 5".match(/\d\s-\s\d/) ); // 1 - 5, also works **A space is a character. Equal in importance with any other character.** -We can't add or remove spaces from a regular expression and expect to work the same. +We can't add or remove spaces from a regular expression and expect it to work the same. In other words, in a regular expression all characters matter, spaces too. ```` @@ -198,6 +198,6 @@ There exist following character classes: ...But that's not all! -Unicode encoding, used by JavaScript for strings, provides many properties for characters, like: which language the letter belongs to (if it's a letter) it is it a punctuation sign, etc. +Unicode encoding, used by JavaScript for strings, provides many properties for characters, like: which language the letter belongs to (if it's a letter), is it a punctuation sign, etc. We can search by these properties as well. That requires flag `pattern:u`, covered in the next article. From 7c73f64a1381ad212843e22d356f7e134c59c821 Mon Sep 17 00:00:00 2001 From: Vse Mozhe Buty Date: Mon, 7 Dec 2020 18:05:47 +0200 Subject: [PATCH 09/31] Replace unicode with Unicode all over the book --- .../03-string/3-truncate/solution.md | 2 +- 1-js/05-data-types/03-string/article.md | 24 +++++++++---------- 4-binary/02-text-decoder/article.md | 2 +- .../01-regexp-introduction/article.md | 2 +- .../03-regexp-unicode/article.md | 8 +++---- .../article.md | 8 +++---- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/1-js/05-data-types/03-string/3-truncate/solution.md b/1-js/05-data-types/03-string/3-truncate/solution.md index 5546c47ee..d51672ae6 100644 --- a/1-js/05-data-types/03-string/3-truncate/solution.md +++ b/1-js/05-data-types/03-string/3-truncate/solution.md @@ -1,6 +1,6 @@ The maximal length must be `maxlength`, so we need to cut it a little shorter, to give space for the ellipsis. -Note that there is actually a single unicode character for an ellipsis. That's not three dots. +Note that there is actually a single Unicode character for an ellipsis. That's not three dots. ```js run demo function truncate(str, maxlength) { diff --git a/1-js/05-data-types/03-string/article.md b/1-js/05-data-types/03-string/article.md index 93c9b8165..4b8e8aa8a 100644 --- a/1-js/05-data-types/03-string/article.md +++ b/1-js/05-data-types/03-string/article.md @@ -50,7 +50,7 @@ let guestList = "Guests: // Error: Unexpected token ILLEGAL Single and double quotes come from ancient times of language creation when the need for multiline strings was not taken into account. Backticks appeared much later and thus are more versatile. -Backticks also allow us to specify a "template function" before the first backtick. The syntax is: func`string`. The function `func` is called automatically, receives the string and embedded expressions and can process them. This is called "tagged templates". This feature makes it easier to implement custom templating, but is rarely used in practice. You can read more about it in the [manual](mdn:/JavaScript/Reference/Template_literals#Tagged_templates). +Backticks also allow us to specify a "template function" before the first backtick. The syntax is: func`string`. The function `func` is called automatically, receives the string and embedded expressions and can process them. This is called "tagged templates". This feature makes it easier to implement custom templating, but is rarely used in practice. You can read more about it in the [manual](mdn:/JavaScript/Reference/Template_literals#Tagged_templates). ## Special characters @@ -86,16 +86,16 @@ Here's the full list: |`\\`|Backslash| |`\t`|Tab| |`\b`, `\f`, `\v`| Backspace, Form Feed, Vertical Tab -- kept for compatibility, not used nowadays. | -|`\xXX`|Unicode character with the given hexadecimal unicode `XX`, e.g. `'\x7A'` is the same as `'z'`.| -|`\uXXXX`|A unicode symbol with the hex code `XXXX` in UTF-16 encoding, for instance `\u00A9` -- is a unicode for the copyright symbol `©`. It must be exactly 4 hex digits. | -|`\u{X…XXXXXX}` (1 to 6 hex characters)|A unicode symbol with the given UTF-32 encoding. Some rare characters are encoded with two unicode symbols, taking 4 bytes. This way we can insert long codes. | +|`\xXX`|Unicode character with the given hexadecimal Unicode `XX`, e.g. `'\x7A'` is the same as `'z'`.| +|`\uXXXX`|A Unicode symbol with the hex code `XXXX` in UTF-16 encoding, for instance `\u00A9` -- is a Unicode for the copyright symbol `©`. It must be exactly 4 hex digits. | +|`\u{X…XXXXXX}` (1 to 6 hex characters)|A Unicode symbol with the given UTF-32 encoding. Some rare characters are encoded with two Unicode symbols, taking 4 bytes. This way we can insert long codes. | -Examples with unicode: +Examples with Unicode: ```js run alert( "\u00A9" ); // © -alert( "\u{20331}" ); // 佫, a rare Chinese hieroglyph (long unicode) -alert( "\u{1F60D}" ); // 😍, a smiling face symbol (another long unicode) +alert( "\u{20331}" ); // 佫, a rare Chinese hieroglyph (long Unicode) +alert( "\u{1F60D}" ); // 😍, a smiling face symbol (another long Unicode) ``` All special characters start with a backslash character `\`. It is also called an "escape character". @@ -499,7 +499,7 @@ All strings are encoded using [UTF-16](https://en.wikipedia.org/wiki/UTF-16). Th alert( String.fromCodePoint(90) ); // Z ``` - We can also add unicode characters by their codes using `\u` followed by the hex code: + We can also add Unicode characters by their codes using `\u` followed by the hex code: ```js run // 90 is 5a in hexadecimal system @@ -608,7 +608,7 @@ In many languages there are symbols that are composed of the base character with For instance, the letter `a` can be the base character for: `àáâäãåā`. Most common "composite" character have their own code in the UTF-16 table. But not all of them, because there are too many possible combinations. -To support arbitrary compositions, UTF-16 allows us to use several unicode characters: the base character followed by one or many "mark" characters that "decorate" it. +To support arbitrary compositions, UTF-16 allows us to use several Unicode characters: the base character followed by one or many "mark" characters that "decorate" it. For instance, if we have `S` followed by the special "dot above" character (code `\u0307`), it is shown as Ṡ. @@ -626,7 +626,7 @@ For example: alert( 'S\u0307\u0323' ); // Ṩ ``` -This provides great flexibility, but also an interesting problem: two characters may visually look the same, but be represented with different unicode compositions. +This provides great flexibility, but also an interesting problem: two characters may visually look the same, but be represented with different Unicode compositions. For instance: @@ -639,7 +639,7 @@ alert( `s1: ${s1}, s2: ${s2}` ); alert( s1 == s2 ); // false though the characters look identical (?!) ``` -To solve this, there exists a "unicode normalization" algorithm that brings each string to the single "normal" form. +To solve this, there exists a "Unicode normalization" algorithm that brings each string to the single "normal" form. It is implemented by [str.normalize()](mdn:js/String/normalize). @@ -663,7 +663,7 @@ If you want to learn more about normalization rules and variants -- they are des - There are 3 types of quotes. Backticks allow a string to span multiple lines and embed expressions `${…}`. - Strings in JavaScript are encoded using UTF-16. -- We can use special characters like `\n` and insert letters by their unicode using `\u...`. +- We can use special characters like `\n` and insert letters by their Unicode using `\u...`. - To get a character, use: `[]`. - To get a substring, use: `slice` or `substring`. - To lowercase/uppercase a string, use: `toLowerCase/toUpperCase`. diff --git a/4-binary/02-text-decoder/article.md b/4-binary/02-text-decoder/article.md index fe9b8c042..3d836e6c0 100644 --- a/4-binary/02-text-decoder/article.md +++ b/4-binary/02-text-decoder/article.md @@ -12,7 +12,7 @@ let decoder = new TextDecoder([label], [options]); - **`label`** -- the encoding, `utf-8` by default, but `big5`, `windows-1251` and many other are also supported. - **`options`** -- optional object: - **`fatal`** -- boolean, if `true` then throw an exception for invalid (non-decodable) characters, otherwise (default) replace them with character `\uFFFD`. - - **`ignoreBOM`** -- boolean, if `true` then ignore BOM (an optional byte-order unicode mark), rarely needed. + - **`ignoreBOM`** -- boolean, if `true` then ignore BOM (an optional byte-order Unicode mark), rarely needed. ...And then decode: diff --git a/9-regular-expressions/01-regexp-introduction/article.md b/9-regular-expressions/01-regexp-introduction/article.md index 1db3f5f9a..ade62cdf2 100644 --- a/9-regular-expressions/01-regexp-introduction/article.md +++ b/9-regular-expressions/01-regexp-introduction/article.md @@ -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 ). `pattern:u` -: Enables full unicode support. The flag enables correct processing of surrogate pairs. More about that in the chapter . +: Enables full Unicode support. The flag enables correct processing of surrogate pairs. More about that in the chapter . `pattern:y` : "Sticky" mode: searching at the exact position in the text (covered in the chapter ) diff --git a/9-regular-expressions/03-regexp-unicode/article.md b/9-regular-expressions/03-regexp-unicode/article.md index 0e0fb88ae..3f23ec5de 100644 --- a/9-regular-expressions/03-regexp-unicode/article.md +++ b/9-regular-expressions/03-regexp-unicode/article.md @@ -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=`, 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": diff --git a/9-regular-expressions/08-regexp-character-sets-and-ranges/article.md b/9-regular-expressions/08-regexp-character-sets-and-ranges/article.md index cb6a27e9d..b24461a90 100644 --- a/9-regular-expressions/08-regexp-character-sets-and-ranges/article.md +++ b/9-regular-expressions/08-regexp-character-sets-and-ranges/article.md @@ -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 . +Of course, we can edit this pattern: add Unicode properties or remove them. Unicode properties are covered in more details in the article . ```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/). From 907e6c83355c88d00ab07d482094816626c1ce5b Mon Sep 17 00:00:00 2001 From: Osvaldo Dias dos Santos Date: Mon, 7 Dec 2020 17:12:39 +0100 Subject: [PATCH 10/31] Fix "polyfills" typo --- 1-js/03-code-quality/06-polyfills/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/03-code-quality/06-polyfills/article.md b/1-js/03-code-quality/06-polyfills/article.md index 81b6d6607..7639b9bb4 100644 --- a/1-js/03-code-quality/06-polyfills/article.md +++ b/1-js/03-code-quality/06-polyfills/article.md @@ -78,7 +78,7 @@ Two interesting libraries of polyfills are: ## Summary -In this chapter we'd like to motivate you to study modern and even "bleeding-edge" langauge features, even if they aren't yet well-supported by JavaScript engines. +In this chapter we'd like to motivate you to study modern and even "bleeding-edge" language features, even if they aren't yet well-supported by JavaScript engines. Just don't forget to use transpiler (if using modern syntax or operators) and polyfills (to add functions that may be missing). And they'll ensure that the code works. From 6a61c04fc51d7a0389ac4c55784eee9ddb7ca25a Mon Sep 17 00:00:00 2001 From: Vse Mozhe Buty Date: Mon, 7 Dec 2020 22:54:54 +0200 Subject: [PATCH 11/31] Fix confusing wording in 9.6 (Word boundary: \b) `\bHello\b` pattern was just analyzed in the previous paragraph, so the current wording is a bit confusing. --- 9-regular-expressions/06-regexp-boundary/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/9-regular-expressions/06-regexp-boundary/article.md b/9-regular-expressions/06-regexp-boundary/article.md index 7c9f442fe..06b5ac9f7 100644 --- a/9-regular-expressions/06-regexp-boundary/article.md +++ b/9-regular-expressions/06-regexp-boundary/article.md @@ -27,7 +27,7 @@ So, it matches the pattern `pattern:\bHello\b`, because: 2. Then matches the word `pattern:Hello`. 3. Then the test `pattern:\b` matches again, as we're between `subject:o` and a comma. -The pattern `pattern:\bHello\b` would also match. But not `pattern:\bHell\b` (because there's no word boundary after `l`) and not `Java!\b` (because the exclamation sign is not a wordly character `pattern:\w`, so there's no word boundary after it). +So the pattern `pattern:\bHello\b` would match, but not `pattern:\bHell\b` (because there's no word boundary after `l`) and not `Java!\b` (because the exclamation sign is not a wordly character `pattern:\w`, so there's no word boundary after it). ```js run alert( "Hello, Java!".match(/\bHello\b/) ); // Hello From 935ed8580ea7321145c1339d8e05d374db28645e Mon Sep 17 00:00:00 2001 From: Vse Mozhe Buty Date: Tue, 8 Dec 2020 00:03:41 +0200 Subject: [PATCH 12/31] Fix typo in 9.7 (Escaping, special characters) --- 9-regular-expressions/07-regexp-escaping/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/9-regular-expressions/07-regexp-escaping/article.md b/9-regular-expressions/07-regexp-escaping/article.md index 7bf989471..eed76791b 100644 --- a/9-regular-expressions/07-regexp-escaping/article.md +++ b/9-regular-expressions/07-regexp-escaping/article.md @@ -96,4 +96,4 @@ alert( "Chapter 5.1".match(regexp) ); // 5.1 - To search for special characters `pattern:[ \ ^ $ . | ? * + ( )` literally, we need to prepend them with a backslash `\` ("escape them"). - We also need to escape `/` if we're inside `pattern:/.../` (but not inside `new RegExp`). -- When passing a string `new RegExp`, we need to double backslashes `\\`, cause string quotes consume one of them. +- When passing a string to `new RegExp`, we need to double backslashes `\\`, cause string quotes consume one of them. From 4a3d842bc6ec08fc277223a61cee7664d2726d32 Mon Sep 17 00:00:00 2001 From: Vse Mozhe Buty Date: Tue, 8 Dec 2020 17:38:02 +0200 Subject: [PATCH 13/31] Fix typo in 9.8 task solution (Sets and ranges [...]) --- .../1-find-range-1/solution.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/9-regular-expressions/08-regexp-character-sets-and-ranges/1-find-range-1/solution.md b/9-regular-expressions/08-regexp-character-sets-and-ranges/1-find-range-1/solution.md index 378471611..85c7748f7 100644 --- a/9-regular-expressions/08-regexp-character-sets-and-ranges/1-find-range-1/solution.md +++ b/9-regular-expressions/08-regexp-character-sets-and-ranges/1-find-range-1/solution.md @@ -5,7 +5,7 @@ Answers: **no, yes**. ```js run alert( "Java".match(/Java[^script]/) ); // null ``` -- Yes, because the part `pattern:[^script]` part matches the character `"S"`. It's not one of `pattern:script`. As the regexp is case-sensitive (no `pattern:i` flag), it treats `"S"` as a different character from `"s"`. +- Yes, because the `pattern:[^script]` part matches the character `"S"`. It's not one of `pattern:script`. As the regexp is case-sensitive (no `pattern:i` flag), it treats `"S"` as a different character from `"s"`. ```js run alert( "JavaScript".match(/Java[^script]/) ); // "JavaS" From b19312e8caf2830716ef9b1d0b80e46d46440616 Mon Sep 17 00:00:00 2001 From: Vse Mozhe Buty Date: Tue, 8 Dec 2020 17:41:33 +0200 Subject: [PATCH 14/31] Update support note in 9.8 (Sets and ranges [...]) See https://caniuse.com/mdn-javascript_builtins_regexp_property_escapes --- .../08-regexp-character-sets-and-ranges/article.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/9-regular-expressions/08-regexp-character-sets-and-ranges/article.md b/9-regular-expressions/08-regexp-character-sets-and-ranges/article.md index b24461a90..a1b8f896d 100644 --- a/9-regular-expressions/08-regexp-character-sets-and-ranges/article.md +++ b/9-regular-expressions/08-regexp-character-sets-and-ranges/article.md @@ -87,8 +87,8 @@ 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 . -```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/). +```warn header="Unicode properties aren't supported in IE" +Unicode properties `pattern:p{…}` are not implemented in IE. If we really need them, we can use library [XRegExp](http://xregexp.com/). Or just use ranges of characters in a language that interests us, e.g. `pattern:[а-я]` for Cyrillic letters. ``` From 656700966b297ddf5edc8e208d4edfc350514dc5 Mon Sep 17 00:00:00 2001 From: Vse Mozhe Buty Date: Wed, 9 Dec 2020 01:45:06 +0200 Subject: [PATCH 15/31] Fix typos in 9.10 (Greedy and lazy quantifiers) --- .../4-find-html-tags-greedy-lazy/task.md | 2 +- .../10-regexp-greedy-and-lazy/article.md | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/9-regular-expressions/10-regexp-greedy-and-lazy/4-find-html-tags-greedy-lazy/task.md b/9-regular-expressions/10-regexp-greedy-and-lazy/4-find-html-tags-greedy-lazy/task.md index 8e96c921d..6759152ff 100644 --- a/9-regular-expressions/10-regexp-greedy-and-lazy/4-find-html-tags-greedy-lazy/task.md +++ b/9-regular-expressions/10-regexp-greedy-and-lazy/4-find-html-tags-greedy-lazy/task.md @@ -12,4 +12,4 @@ let str = '<> '; alert( str.match(regexp) ); // '', '', '' ``` -Here we assume that tag attributes may not contain `<` and `>` (inside squotes too), that simplifies things a bit. +Here we assume that tag attributes may not contain `<` and `>` (inside quotes too), that simplifies things a bit. diff --git a/9-regular-expressions/10-regexp-greedy-and-lazy/article.md b/9-regular-expressions/10-regexp-greedy-and-lazy/article.md index 79abc559d..2f656479d 100644 --- a/9-regular-expressions/10-regexp-greedy-and-lazy/article.md +++ b/9-regular-expressions/10-regexp-greedy-and-lazy/article.md @@ -88,7 +88,7 @@ These common words do not make it obvious why the regexp fails, so let's elabora That's probably not what we expected, but that's how it works. -**In the greedy mode (by default) a quantifier is repeated as many times as possible.** +**In the greedy mode (by default) a quantified character is repeated as many times as possible.** The regexp engine adds to the match as many characters as it can for `pattern:.+`, and then shortens that one by one, if the rest of the pattern doesn't match. @@ -109,7 +109,7 @@ let regexp = /".+?"/g; let str = 'a "witch" and her "broom" is one'; -alert( str.match(regexp) ); // witch, broom +alert( str.match(regexp) ); // "witch", "broom" ``` To clearly understand the change, let's trace the search step by step. @@ -179,7 +179,7 @@ let regexp = /"[^"]+"/g; let str = 'a "witch" and her "broom" is one'; -alert( str.match(regexp) ); // witch, broom +alert( str.match(regexp) ); // "witch", "broom" ``` The regexp `pattern:"[^"]+"` gives correct results, because it looks for a quote `pattern:'"'` followed by one or more non-quotes `pattern:[^"]`, and then the closing quote. @@ -293,9 +293,9 @@ alert( str2.match(regexp) ); // , Date: Wed, 9 Dec 2020 18:54:47 +0200 Subject: [PATCH 16/31] Fix typos in 9.11 (Capturing groups) --- 9-regular-expressions/11-regexp-groups/01-test-mac/task.md | 2 +- .../11-regexp-groups/03-find-decimal-numbers/solution.md | 2 +- .../11-regexp-groups/04-parse-expression/solution.md | 2 +- 9-regular-expressions/11-regexp-groups/article.md | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/9-regular-expressions/11-regexp-groups/01-test-mac/task.md b/9-regular-expressions/11-regexp-groups/01-test-mac/task.md index 029a4803a..a2e799cfa 100644 --- a/9-regular-expressions/11-regexp-groups/01-test-mac/task.md +++ b/9-regular-expressions/11-regexp-groups/01-test-mac/task.md @@ -16,5 +16,5 @@ alert( regexp.test('0132546789AB') ); // false (no colons) alert( regexp.test('01:32:54:67:89') ); // false (5 numbers, must be 6) -alert( regexp.test('01:32:54:67:89:ZZ') ) // false (ZZ ad the end) +alert( regexp.test('01:32:54:67:89:ZZ') ) // false (ZZ at the end) ``` diff --git a/9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/solution.md b/9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/solution.md index c4349f9a0..813d619ef 100644 --- a/9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/solution.md +++ b/9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/solution.md @@ -1,4 +1,4 @@ -A positive number with an optional decimal part is (per previous task): `pattern:\d+(\.\d+)?`. +A positive number with an optional decimal part is: `pattern:\d+(\.\d+)?`. Let's add the optional `pattern:-` in the beginning: diff --git a/9-regular-expressions/11-regexp-groups/04-parse-expression/solution.md b/9-regular-expressions/11-regexp-groups/04-parse-expression/solution.md index 130c57be3..ac67519bb 100644 --- a/9-regular-expressions/11-regexp-groups/04-parse-expression/solution.md +++ b/9-regular-expressions/11-regexp-groups/04-parse-expression/solution.md @@ -1,4 +1,4 @@ -A regexp for a number is: `pattern:-?\d+(\.\d+)?`. We created it in previous tasks. +A regexp for a number is: `pattern:-?\d+(\.\d+)?`. We created it in the previous task. An operator is `pattern:[-+*/]`. The hyphen `pattern:-` goes first in the square brackets, because in the middle it would mean a character range, while we just want a character `-`. diff --git a/9-regular-expressions/11-regexp-groups/article.md b/9-regular-expressions/11-regexp-groups/article.md index e559fd87c..e0ef00bd3 100644 --- a/9-regular-expressions/11-regexp-groups/article.md +++ b/9-regular-expressions/11-regexp-groups/article.md @@ -249,7 +249,7 @@ The call to `matchAll` does not perform the search. Instead, it returns an itera So, there will be found as many results as needed, not more. -E.g. there are potentially 100 matches in the text, but in a `for..of` loop we found 5 of them, then decided it's enough and make a `break`. Then the engine won't spend time finding other 95 matches. +E.g. there are potentially 100 matches in the text, but in a `for..of` loop we found 5 of them, then decided it's enough and made a `break`. Then the engine won't spend time finding other 95 matches. ``` ## Named groups From 276f48de2b57896aae2fe8456b9260deb77c3a67 Mon Sep 17 00:00:00 2001 From: Vse Mozhe Buty Date: Wed, 9 Dec 2020 21:19:50 +0200 Subject: [PATCH 17/31] Fix typos in 9.13 (Alternation (OR) |) --- 9-regular-expressions/13-regexp-alternation/article.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/9-regular-expressions/13-regexp-alternation/article.md b/9-regular-expressions/13-regexp-alternation/article.md index 007d8f6a6..0fe2175c7 100644 --- a/9-regular-expressions/13-regexp-alternation/article.md +++ b/9-regular-expressions/13-regexp-alternation/article.md @@ -20,7 +20,7 @@ alert( str.match(regexp) ); // 'HTML', 'CSS', 'JavaScript' We already saw a similar thing -- square brackets. They allow to choose between multiple characters, for instance `pattern:gr[ae]y` matches `match:gray` or `match:grey`. -Square brackets allow only characters or character sets. Alternation allows any expressions. A regexp `pattern:A|B|C` means one of expressions `A`, `B` or `C`. +Square brackets allow only characters or character classes. Alternation allows any expressions. A regexp `pattern:A|B|C` means one of expressions `A`, `B` or `C`. For instance: @@ -33,7 +33,7 @@ To apply alternation to a chosen part of the pattern, we can enclose it in paren ## Example: regexp for time -In previous articles there was a task to build a regexp for searching time in the form `hh:mm`, for instance `12:00`. But a simple `pattern:\d\d:\d\d` is too vague. It accepts `25:99` as the time (as 99 seconds match the pattern, but that time is invalid). +In previous articles there was a task to build a regexp for searching time in the form `hh:mm`, for instance `12:00`. But a simple `pattern:\d\d:\d\d` is too vague. It accepts `25:99` as the time (as 99 minutes match the pattern, but that time is invalid). How can we make a better pattern? @@ -47,7 +47,7 @@ We can write both variants in a regexp using alternation: `pattern:[01]\d|2[0-3] Next, minutes must be from `00` to `59`. In the regular expression language that can be written as `pattern:[0-5]\d`: the first digit `0-5`, and then any digit. -If we glue minutes and seconds together, we get the pattern: `pattern:[01]\d|2[0-3]:[0-5]\d`. +If we glue hours and minutes together, we get the pattern: `pattern:[01]\d|2[0-3]:[0-5]\d`. We're almost done, but there's a problem. The alternation `pattern:|` now happens to be between `pattern:[01]\d` and `pattern:2[0-3]:[0-5]\d`. From 6fc5b2c55526ea1fd5fb1f13dea47beb89d8470a Mon Sep 17 00:00:00 2001 From: Vse Mozhe Buty Date: Wed, 9 Dec 2020 22:37:29 +0200 Subject: [PATCH 18/31] Correct example in 9.14 (Lookahead and lookbehind) Without the `g` flag, we cannot say that the price is skipped. Without the `\b` assertion, we will have the part of the price in the result. --- .../14-regexp-lookahead-lookbehind/article.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/9-regular-expressions/14-regexp-lookahead-lookbehind/article.md b/9-regular-expressions/14-regexp-lookahead-lookbehind/article.md index 48c82da14..9f4f4f6c6 100644 --- a/9-regular-expressions/14-regexp-lookahead-lookbehind/article.md +++ b/9-regular-expressions/14-regexp-lookahead-lookbehind/article.md @@ -54,7 +54,7 @@ The syntax is: `pattern:X(?!Y)`, it means "search `pattern:X`, but only if not f ```js run let str = "2 turkeys cost 60€"; -alert( str.match(/\d+(?!€)/) ); // 2 (the price is skipped) +alert( str.match(/\d+\b(?!€)/g) ); // 2 (the price is not matched) ``` ## Lookbehind @@ -81,7 +81,7 @@ And, if we need the quantity -- a number, not preceded by `subject:$`, then we c ```js run let str = "2 turkeys cost $60"; -alert( str.match(/(? Date: Thu, 10 Dec 2020 00:18:02 +0200 Subject: [PATCH 19/31] Make solution in 9.14 safer As the task uses multiline HTML and the note at the end suggests using `s` flag, the lazy quantifier would be safer. --- .../2-insert-after-head/solution.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/9-regular-expressions/14-regexp-lookahead-lookbehind/2-insert-after-head/solution.md b/9-regular-expressions/14-regexp-lookahead-lookbehind/2-insert-after-head/solution.md index b5915744a..f2bbd3f5b 100644 --- a/9-regular-expressions/14-regexp-lookahead-lookbehind/2-insert-after-head/solution.md +++ b/9-regular-expressions/14-regexp-lookahead-lookbehind/2-insert-after-head/solution.md @@ -1,4 +1,4 @@ -In order to insert after the `` tag, we must first find it. We can use the regular expression pattern `pattern:` for that. +In order to insert after the `` tag, we must first find it. We can use the regular expression pattern `pattern:` for that. In this task we don't need to modify the `` tag. We only need to add the text after it. @@ -6,18 +6,18 @@ Here's how we can do it: ```js run let str = '......'; -str = str.replace(//, '$&

Hello

'); +str = str.replace(//, '$&

Hello

'); alert(str); // ...

Hello

... ``` -In the replacement string `$&` means the match itself, that is, the part of the source text that corresponds to `pattern:`. It gets replaced by itself plus `

Hello

`. +In the replacement string `$&` means the match itself, that is, the part of the source text that corresponds to `pattern:`. It gets replaced by itself plus `

Hello

`. An alternative is to use lookbehind: ```js run let str = '......'; -str = str.replace(/(?<=)/, `

Hello

`); +str = str.replace(/(?<=)/, `

Hello

`); alert(str); // ...

Hello

... ``` @@ -26,11 +26,11 @@ As you can see, there's only lookbehind part in this regexp. It works like this: - At every position in the text. -- Check if it's preceeded by `pattern:`. +- Check if it's preceeded by `pattern:`. - If it's so then we have the match. -The tag `pattern:` won't be returned. The result of this regexp is literally an empty string, but it matches only at positions preceeded by `pattern:`. +The tag `pattern:` won't be returned. The result of this regexp is literally an empty string, but it matches only at positions preceeded by `pattern:`. -So we replaces the "empty line", preceeded by `pattern:`, with `

Hello

`. That's the insertion after ``. +So we replaces the "empty line", preceeded by `pattern:`, with `

Hello

`. That's the insertion after ``. -P.S. Regexp flags, such as `pattern:s` and `pattern:i` can also useful: `pattern://si`. The `pattern:s` flag makes the dot `pattern:.` match a newline character, and `pattern:i` flag makes `pattern:` also match `match:` case-insensitively. +P.S. Regexp flags, such as `pattern:s` and `pattern:i` can also be useful: `pattern://si`. The `pattern:s` flag makes the dot `pattern:.` match a newline character, and `pattern:i` flag makes `pattern:` also match `match:` case-insensitively. From ec15bd2950b4b0e6038dccc4310fdbf5c820783b Mon Sep 17 00:00:00 2001 From: jason Date: Thu, 10 Dec 2020 14:29:53 +0800 Subject: [PATCH 20/31] Fix typo in 1.99.1 (Proxy and Reflect) --- 1-js/99-js-misc/01-proxy/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/99-js-misc/01-proxy/article.md b/1-js/99-js-misc/01-proxy/article.md index c5b027048..1f84912e5 100644 --- a/1-js/99-js-misc/01-proxy/article.md +++ b/1-js/99-js-misc/01-proxy/article.md @@ -67,7 +67,7 @@ For every internal method, there's a trap in this table: the name of the method | `[[PreventExtensions]]` | `preventExtensions` | [Object.preventExtensions](mdn:/JavaScript/Reference/Global_Objects/Object/preventExtensions) | | `[[DefineOwnProperty]]` | `defineProperty` | [Object.defineProperty](mdn:/JavaScript/Reference/Global_Objects/Object/defineProperty), [Object.defineProperties](mdn:/JavaScript/Reference/Global_Objects/Object/defineProperties) | | `[[GetOwnProperty]]` | `getOwnPropertyDescriptor` | [Object.getOwnPropertyDescriptor](mdn:/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor), `for..in`, `Object.keys/values/entries` | -| `[[OwnPropertyKeys]]` | `ownKeys` | [Object.getOwnPropertyNames](mdn:/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames), [Object.getOwnPropertySymbols](mdn:/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols), `for..in`, `Object/keys/values/entries` | +| `[[OwnPropertyKeys]]` | `ownKeys` | [Object.getOwnPropertyNames](mdn:/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames), [Object.getOwnPropertySymbols](mdn:/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols), `for..in`, `Object.keys/values/entries` | ```warn header="Invariants" JavaScript enforces some invariants -- conditions that must be fulfilled by internal methods and traps. From e5d1c02628edbd8684241049bc132d3d54c451be Mon Sep 17 00:00:00 2001 From: Vse Mozhe Buty Date: Thu, 10 Dec 2020 16:00:34 +0200 Subject: [PATCH 21/31] Fix typo, clarify wording in 9.15 (Catastrophic backtracking) --- .../15-regexp-catastrophic-backtracking/article.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/9-regular-expressions/15-regexp-catastrophic-backtracking/article.md b/9-regular-expressions/15-regexp-catastrophic-backtracking/article.md index 85343bf84..6913a0178 100644 --- a/9-regular-expressions/15-regexp-catastrophic-backtracking/article.md +++ b/9-regular-expressions/15-regexp-catastrophic-backtracking/article.md @@ -160,7 +160,7 @@ Trying each of them is exactly the reason why the search takes so long. ## Back to words and strings -The similar thing happens in our first example, when we look words by pattern `pattern:^(\w+\s?)*$` in the string `subject:An input that hangs!`. +The similar thing happens in our first example, when we look for words by pattern `pattern:^(\w+\s?)*$` in the string `subject:An input that hangs!`. The reason is that a word can be represented as one `pattern:\w+` or many: @@ -238,7 +238,7 @@ E.g. in the regexp `pattern:(\d+)*$` it's obvious for a human, that `pattern:+` (1234)(56789)! ``` -And in the original example `pattern:^(\w+\s?)*$` we may want to forbid backtracking in `pattern:\w+`. That is: `pattern:\w+` should match a whole word, with the maximal possible length. There's no need to lower the repetitions count in `pattern:\w+`, try to split it into two words `pattern:\w+\w+` and so on. +And in the original example `pattern:^(\w+\s?)*$` we may want to forbid backtracking in `pattern:\w+`. That is: `pattern:\w+` should match a whole word, with the maximal possible length. There's no need to lower the repetitions count in `pattern:\w+` or to split it into two words `pattern:\w+\w+` and so on. Modern regular expression engines support possessive quantifiers for that. Regular quantifiers become possessive if we add `pattern:+` after them. That is, we use `pattern:\d++` instead of `pattern:\d+` to stop `pattern:+` from backtracking. From 4512a02fd027a223f496f84bf1ab6541da365fbc Mon Sep 17 00:00:00 2001 From: Vse Mozhe Buty Date: Thu, 10 Dec 2020 16:59:05 +0200 Subject: [PATCH 22/31] Update support note in 9.15 (Catastrophic backtracking) Refs: https://bugs.chromium.org/p/v8/issues/detail?id=10765 Refs: https://bugs.chromium.org/p/v8/issues/detail?id=11021 Can be tested in the Goole Chrome Beta, Dev, and Canary branches now, as well as in the Node.js v8-canary branch. --- .../15-regexp-catastrophic-backtracking/article.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/9-regular-expressions/15-regexp-catastrophic-backtracking/article.md b/9-regular-expressions/15-regexp-catastrophic-backtracking/article.md index 85343bf84..080f92af2 100644 --- a/9-regular-expressions/15-regexp-catastrophic-backtracking/article.md +++ b/9-regular-expressions/15-regexp-catastrophic-backtracking/article.md @@ -37,7 +37,7 @@ let str = "An input string that takes a long time or even makes this regexp hang alert( regexp.test(str) ); ``` -To be fair, let's note that some regular expression engines can handle such a search effectively. But most of them can't. Browser engines usually hang. +To be fair, let's note that some regular expression engines can handle such a search effectively. But most of them can't. Browser engines usually hang (Google Chrome can handle such a search since version 88 due to a new experimental regular expression engine shipped in V8, JavaScript engine for browsers and Node.js). ## Simplified example @@ -74,7 +74,7 @@ Here's what the regexp engine does: ``` After all digits are consumed, `pattern:\d+` is considered found (as `match:123456789`). - + Then the star quantifier `pattern:(\d+)*` applies. But there are no more digits in the text, so the star doesn't give anything. The next character in the pattern is the string end `pattern:$`. But in the text we have `subject:z` instead, so there's no match: @@ -220,7 +220,7 @@ The time needed to try a lot of (actually most of) combinations is now saved. ## Preventing backtracking -It's not always convenient to rewrite a regexp though. In the example above it was easy, but it's not always obvious how to do it. +It's not always convenient to rewrite a regexp though. In the example above it was easy, but it's not always obvious how to do it. Besides, a rewritten regexp is usually more complex, and that's not good. Regexps are complex enough without extra efforts. @@ -246,7 +246,7 @@ Possessive quantifiers are in fact simpler than "regular" ones. They just match There are also so-called "atomic capturing groups" - a way to disable backtracking inside parentheses. -...But the bad news is that, unfortunately, in JavaScript they are not supported. +...But the bad news is that, unfortunately, in JavaScript they are not supported. We can emulate them though using a "lookahead transform". From 04ce7283ad159f7583be52449b7b6e989157a54c Mon Sep 17 00:00:00 2001 From: Vse Mozhe Buty Date: Thu, 10 Dec 2020 20:42:09 +0200 Subject: [PATCH 23/31] Fix typo in 9.16 (Sticky flag "y"...) --- 9-regular-expressions/16-regexp-sticky/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/9-regular-expressions/16-regexp-sticky/article.md b/9-regular-expressions/16-regexp-sticky/article.md index 373f3453e..ece2c960f 100644 --- a/9-regular-expressions/16-regexp-sticky/article.md +++ b/9-regular-expressions/16-regexp-sticky/article.md @@ -93,7 +93,7 @@ The result is correct. ...But wait, not so fast. -Please note: the `regexp.exec` call start searching at position `lastIndex` and then goes further. If there's no word at position `lastIndex`, but it's somewhere after it, then it will be found: +Please note: the `regexp.exec` call starts searching at position `lastIndex` and then goes further. If there's no word at position `lastIndex`, but it's somewhere after it, then it will be found: ```js run let str = 'let varName = "value"'; From a8c22e92e64d6292997ffff4fb6b9f1683edc6ea Mon Sep 17 00:00:00 2001 From: Vse Mozhe Buty Date: Fri, 11 Dec 2020 00:40:16 +0200 Subject: [PATCH 24/31] Fix typo in 9.17 (Methods of RegExp and String) --- 9-regular-expressions/17-regexp-methods/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/9-regular-expressions/17-regexp-methods/article.md b/9-regular-expressions/17-regexp-methods/article.md index a7847c0f6..9654b1423 100644 --- a/9-regular-expressions/17-regexp-methods/article.md +++ b/9-regular-expressions/17-regexp-methods/article.md @@ -142,7 +142,7 @@ To find all hyphens, we need to use not the string `"-"`, but a regexp `pattern: alert( '12-34-56'.replace( *!*/-/g*/!*, ":" ) ) // 12:34:56 ``` -The second argument is a replacement string. We can use special character in it: +The second argument is a replacement string. We can use special characters in it: | Symbols | Action in the replacement string | |--------|--------| From eb5e7da359e8ae74ad02c0e069d1e20f670724c7 Mon Sep 17 00:00:00 2001 From: Raphael <48417580+RapTho@users.noreply.github.com> Date: Sun, 13 Dec 2020 12:33:00 +0100 Subject: [PATCH 25/31] Fixed typo --- 5-network/02-formdata/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/5-network/02-formdata/article.md b/5-network/02-formdata/article.md index 2a2cf6840..a3d924528 100644 --- a/5-network/02-formdata/article.md +++ b/5-network/02-formdata/article.md @@ -47,7 +47,7 @@ As you can see, that's almost one-liner: ``` -In this example, the server code is not presented, as it's beyound our scope. The server accepts the POST request and replies "User saved". +In this example, the server code is not presented, as it's beyond our scope. The server accepts the POST request and replies "User saved". ## FormData Methods From 27c30303fab0157b623a054495ede4796fe7c9f0 Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Sun, 13 Dec 2020 20:15:24 +0300 Subject: [PATCH 26/31] minor fixes --- .../15-regexp-catastrophic-backtracking/article.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/9-regular-expressions/15-regexp-catastrophic-backtracking/article.md b/9-regular-expressions/15-regexp-catastrophic-backtracking/article.md index 080f92af2..3411e7eb2 100644 --- a/9-regular-expressions/15-regexp-catastrophic-backtracking/article.md +++ b/9-regular-expressions/15-regexp-catastrophic-backtracking/article.md @@ -37,7 +37,9 @@ let str = "An input string that takes a long time or even makes this regexp hang alert( regexp.test(str) ); ``` -To be fair, let's note that some regular expression engines can handle such a search effectively. But most of them can't. Browser engines usually hang (Google Chrome can handle such a search since version 88 due to a new experimental regular expression engine shipped in V8, JavaScript engine for browsers and Node.js). +To be fair, let's note that some regular expression engines can handle such a search effectively, while some can't. + +For example, V8 engine version starting from 8.8 can handle such a search (so Google Chrome 88 doesn't hang here). ## Simplified example From f716a917bce18c4858e17fafb1e478a20c5f7c6b Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Sun, 13 Dec 2020 20:16:17 +0300 Subject: [PATCH 27/31] minor fixes --- .../15-regexp-catastrophic-backtracking/article.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/9-regular-expressions/15-regexp-catastrophic-backtracking/article.md b/9-regular-expressions/15-regexp-catastrophic-backtracking/article.md index 3411e7eb2..16063b157 100644 --- a/9-regular-expressions/15-regexp-catastrophic-backtracking/article.md +++ b/9-regular-expressions/15-regexp-catastrophic-backtracking/article.md @@ -37,9 +37,7 @@ let str = "An input string that takes a long time or even makes this regexp hang alert( regexp.test(str) ); ``` -To be fair, let's note that some regular expression engines can handle such a search effectively, while some can't. - -For example, V8 engine version starting from 8.8 can handle such a search (so Google Chrome 88 doesn't hang here). +To be fair, let's note that some regular expression engines can handle such a search effectively, for example V8 engine version starting from 8.8 can do that (so Google Chrome 88 doesn't hang here), while Firefox browser does hang. ## Simplified example From 8bc2e921eb1b5f6c13fe092e9714a99c1bf1d4d9 Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Sun, 13 Dec 2020 20:35:12 +0300 Subject: [PATCH 28/31] minor fixes --- 2-ui/99-ui-misc/02-selection-range/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2-ui/99-ui-misc/02-selection-range/article.md b/2-ui/99-ui-misc/02-selection-range/article.md index ac7b46da9..8699bd117 100644 --- a/2-ui/99-ui-misc/02-selection-range/article.md +++ b/2-ui/99-ui-misc/02-selection-range/article.md @@ -211,7 +211,7 @@ With these methods we can do basically anything with selected nodes. Here's the test stand to see them in action: -```html run autorun height=260 +```html run refresh autorun height=260 Click buttons to run methods on the selection, "resetExample" to reset it.

Example: italic and bold

From c275ba48b66d165d885decb220f130d93d832acd Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Sun, 13 Dec 2020 21:17:05 +0300 Subject: [PATCH 29/31] closes #2382 --- .../17-regexp-methods/article.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/9-regular-expressions/17-regexp-methods/article.md b/9-regular-expressions/17-regexp-methods/article.md index 9654b1423..0d5168b40 100644 --- a/9-regular-expressions/17-regexp-methods/article.md +++ b/9-regular-expressions/17-regexp-methods/article.md @@ -228,6 +228,23 @@ alert(result); // Smith, John Using a function gives us the ultimate replacement power, because it gets all the information about the match, has access to outer variables and can do everything. +## str.replaceAll(str|regexp, str|func) + +This method is essentially the same as `str.replace`, with two major differences: + +1. If the first argument is a string, it replaces *all occurences* of the string, while `replace` replaces only the *first occurence*. +2. If the first argument is a regular expression without the `g` flag, there'll be an error. With `g` flag, it works the same as `replace`. + +The main use case for `replaceAll` is replacing all occurences of a string. + +Like this: + +```js run +// replace all dashes by a colon +alert('12-34-56'.replaceAll("-", ":")) // 12:34:56 +``` + + ## regexp.exec(str) The method `regexp.exec(str)` method returns a match for `regexp` in the string `str`. Unlike previous methods, it's called on a regexp, not on a string. From 37e51e0d134132103994f9c7e8a6dc64cda1efea Mon Sep 17 00:00:00 2001 From: Pano Papadatos Date: Sun, 13 Dec 2020 13:35:25 -0500 Subject: [PATCH 30/31] Typo "in any *of* language" --- 9-regular-expressions/03-regexp-unicode/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/9-regular-expressions/03-regexp-unicode/article.md b/9-regular-expressions/03-regexp-unicode/article.md index 3f23ec5de..3e981c80a 100644 --- a/9-regular-expressions/03-regexp-unicode/article.md +++ b/9-regular-expressions/03-regexp-unicode/article.md @@ -39,7 +39,7 @@ For instance, if a character has `Letter` property, it means that the character We can search for characters with a property, written as `pattern:\p{…}`. To use `pattern:\p{…}`, a regular expression must have flag `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. +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. In the example below three kinds of letters will be found: English, Georgian and Korean. From 11edd5393609410a72e5e056db448b9e0ba06d09 Mon Sep 17 00:00:00 2001 From: Osvaldo Dias dos Santos Date: Mon, 14 Dec 2020 18:48:01 +0100 Subject: [PATCH 31/31] Resolve conflicts. --- 4-binary/02-text-decoder/article.md | 2 +- .../02-regexp-character-classes/article.md | 29 ++++--------------- .../17-regexp-methods/article.md | 6 +--- 3 files changed, 7 insertions(+), 30 deletions(-) diff --git a/4-binary/02-text-decoder/article.md b/4-binary/02-text-decoder/article.md index cb761ec3d..3d836e6c0 100644 --- a/4-binary/02-text-decoder/article.md +++ b/4-binary/02-text-decoder/article.md @@ -2,7 +2,7 @@ What if the binary data is actually a string? For instance, we received a file with textual data. -The build-in [TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder) object allows to read the value into an an actual JavaScript string, given the buffer and the encoding. +The build-in [TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder) object allows to read the value into an actual JavaScript string, given the buffer and the encoding. We first need to create it: ```js diff --git a/9-regular-expressions/02-regexp-character-classes/article.md b/9-regular-expressions/02-regexp-character-classes/article.md index 5b02c5601..6917b29f4 100644 --- a/9-regular-expressions/02-regexp-character-classes/article.md +++ b/9-regular-expressions/02-regexp-character-classes/article.md @@ -8,11 +8,7 @@ Uma *classe de caracteres* é uma notação especial que corresponde a qualquer Para começar, vamos explorar a classe "digit". Está escrito como `padrão: \d` e corresponde a "qualquer dígito único". -<<<<<<< HEAD Por exemplo, vamos encontrar o primeiro dígito no número de telefone: -======= -For instance, let's find the first digit in the phone number: ->>>>>>> 23e85b3c33762347e26276ed869e491e959dd557 ```js run let str = "+7(903)-123-45-67"; @@ -148,17 +144,10 @@ Há muitas situações em que gostaríamos que um ponto significasse literalment alert( "A\nB".match(/A.B/s) ); // A\nB (correspondência!) ``` -<<<<<<< HEAD -````warn header="Não suportado no Firefox, IE, Edge" -Verifique para obter o estado de suporte mais recente. No momento da redação deste documento, não inclui o Firefox, IE, Edge. +````warn header="Não suportado no IE" +A flag `padrão:s` não é suportada no IE. -Felizmente, há uma alternativa, que funciona em qualquer lugar. Podemos usar uma regexp como `padrão:[\s\S]` para corresponder a "qualquer caractere". -======= -````warn header="Not supported in IE" -The `pattern:s` flag is not supported in IE. - -Luckily, there's an alternative, that works everywhere. We can use a regexp like `pattern:[\s\S]` to match "any character" (this pattern will be covered in the article ). ->>>>>>> 23e85b3c33762347e26276ed869e491e959dd557 +Felizmente, há uma alternativa, que funciona em qualquer lugar. Podemos usar uma regexp como `padrão:[\s\S]` para corresponder a "qualquer caractere" (este padrão irá ser estudado no artigo ). ```js run alert( "A\nB".match(/A[\s\S]B/) ); // A\nB (match!) @@ -190,11 +179,7 @@ alert( "1 - 5".match(/\d\s-\s\d/) ); // 1 - 5, também funciona **Um espaço é um caractere. Igual em importância com qualquer outro caractere.** -<<<<<<< HEAD -Não podemos adicionar ou remover espaços de uma expressão regular e esperamos funcionar da mesma maneira. -======= -We can't add or remove spaces from a regular expression and expect it to work the same. ->>>>>>> 23e85b3c33762347e26276ed869e491e959dd557 +Não podemos adicionar ou remover espaços de uma expressão regular e esperar que funcione da mesma maneira. Em outras palavras, em uma expressão regular, todos os caracteres são importantes, espaços também. ```` @@ -213,10 +198,6 @@ Existem as seguintes classes de caracteres: ...Mas isso não é tudo! -<<<<<<< HEAD -A codificação unicode, usada pelo JavaScript para strings, fornece muitas propriedades para caracteres, como: a qual idioma a letra pertence (se é uma letra), é um sinal de pontuação etc. -======= -Unicode encoding, used by JavaScript for strings, provides many properties for characters, like: which language the letter belongs to (if it's a letter), is it a punctuation sign, etc. ->>>>>>> 23e85b3c33762347e26276ed869e491e959dd557 +A codificação unicode, usada pelo JavaScript para strings, fornece muitas propriedades para caracteres, como: a qual idioma a letra pertence (se é uma letra), será que é um sinal de pontuação, etc. Também podemos pesquisar por essas propriedades. Isso requer a flag `padrão:u`, abordada no próximo artigo. diff --git a/9-regular-expressions/17-regexp-methods/article.md b/9-regular-expressions/17-regexp-methods/article.md index 5c0e9b26d..8b4683a21 100644 --- a/9-regular-expressions/17-regexp-methods/article.md +++ b/9-regular-expressions/17-regexp-methods/article.md @@ -142,11 +142,7 @@ Para encontrar todos os hífens, não precisamos usar a string `"-"`, mas a rege alert( '12-34-56'.replace( /-/g, ":" ) ) // 12:34:56 ``` -<<<<<<< HEAD -O segundo argumento é uma string substituta. Podemos usar caracteres especiais: -======= -The second argument is a replacement string. We can use special characters in it: ->>>>>>> 23e85b3c33762347e26276ed869e491e959dd557 +O segundo argumento é uma string substituta. Podemos usar caracteres especiais nela: | Símbolos | Ação na string substituta | |--------|--------|