diff --git a/5-network/05-fetch-api/article.md b/5-network/05-fetch-api/article.md index 7b0330f2..441273a6 100644 --- a/5-network/05-fetch-api/article.md +++ b/5-network/05-fetch-api/article.md @@ -1,20 +1,21 @@ # Fetch API -So far, we know quite a bit about fetch. +So far, we know quite a bit about `fetch`. Now let's see the rest of API, to cover all its abilities. -Here's the full list of all possible fetch options with their default values (alternatives in comments): +Here's the full list of all possible `fetch` options with their default values (alternatives in comments): ```js let promise = fetch(url, { method: "GET", // POST, PUT, DELETE, etc. headers: { - "Content-Type": "text/plain;charset=UTF-8" // for a string body, depends on body + // the content type header value is usually auto-set depending on the request body + "Content-Type": "text/plain;charset=UTF-8" }, body: undefined // string, FormData, Blob, BufferSource, or URLSearchParams - referrer: "about:client", // "" for no-referrer, or an url from the current origin + referrer: "about:client", // or "" to send no Referer header, or an url from the current origin referrerPolicy: "no-referrer-when-downgrade", // no-referrer, origin, same-origin... mode: "cors", // same-origin, no-cors credentials: "same-origin", // omit, include @@ -39,8 +40,7 @@ Now let's explore the rest of options. These options govern how `fetch` sets HTTP `Referer` header. -That header contains the url of the page that made the request. In most scenarios, it plays a very minor informational role, but sometimes, for security purposes, it makes sense to remove or modify it. -. +That header contains the url of the page that made the request. In most scenarios, it plays a very minor informational role, but sometimes, for security purposes, it makes sense to remove or shorten it. **The `referrer` option allows to set any `Referer` within the current origin) or disable it.** @@ -72,13 +72,13 @@ Possible values are described in the [Referrer Policy specification](https://w3c - **`"no-referrer-when-downgrade"`** -- default value: `Referer` is sent always, unless we send a request from HTTPS to HTTP (to less secure protocol). - **`"no-referrer"`** -- never send `Referer`. - **`"origin"`** -- only send the origin in `Referer`, not the full page URL, e.g. `http://site.com` instead of `http://site.com/path`. -- **`"origin-when-cross-origin"`** -- send full referrer to the same origin, but only the origin part for cross-origin requests. -- **`"same-origin"`** -- send full referrer to the same origin, but no referer for for cross-origin requests. -- **`"strict-origin"`** -- send only origin, don't send referrer for HTTPS→HTTP requests. -- **`"strict-origin-when-cross-origin"`** -- for same-origin send full referrer, for cross-origin send only origin, unless it's HTTPS→HTTP request, then send nothing. +- **`"origin-when-cross-origin"`** -- send full `Referer` to the same origin, but only the origin part for cross-origin requests. +- **`"same-origin"`** -- send full `Referer` to the same origin, but no referer for for cross-origin requests. +- **`"strict-origin"`** -- send only origin, don't send `Referer` for HTTPS→HTTP requests. +- **`"strict-origin-when-cross-origin"`** -- for same-origin send full `Referer`, for cross-origin send only origin, unless it's HTTPS→HTTP request, then send nothing. - **`"unsafe-url"`** -- always send full url in `Referer`. -Let's say we have an admin zone with URL structure that shouldn't be visible from outside. +Let's say we have an admin zone with URL structure that shouldn't be known from outside of the site. If we send a cross-origin `fetch`, then by default it sends the `Referer` header with the full url of our page (except when we request from HTTPS to HTTP, then no `Referer`). @@ -92,7 +92,7 @@ fetch('https://another.com/page', { }); ``` -Otherwise, if we'd like the remote side to see where the request comes from, we can send only the "origin" part of the url: +Otherwise, if we'd like the remote side to see only the domain where the request comes from, but not the full URL, we can send only the "origin" part of it: ```js fetch('https://another.com/page', { diff --git a/9-regular-expressions/09-regexp-groups/3-find-decimal-positive-numbers/solution.md b/9-regular-expressions/09-regexp-groups/3-find-decimal-positive-numbers/solution.md index 23065413..c99a95bb 100644 --- a/9-regular-expressions/09-regexp-groups/3-find-decimal-positive-numbers/solution.md +++ b/9-regular-expressions/09-regexp-groups/3-find-decimal-positive-numbers/solution.md @@ -1,11 +1,11 @@ -An non-negative integer number is `pattern:\d+`. We should exclude `0` as the first digit, as we don't need zero, but we can allow it in further digits. +An non-negative integer number is `pattern:\d+`. A zero `0` can't be the first digit, but we should allow it in further digits. So that gives us `pattern:[1-9]\d*`. A decimal part is: `pattern:\.\d+`. -Because the decimal part is optional, let's put it in parentheses with the quantifier `pattern:'?'`. +Because the decimal part is optional, let's put it in parentheses with the quantifier `pattern:?`. Finally we have the regexp: `pattern:[1-9]\d*(\.\d+)?`: diff --git a/9-regular-expressions/09-regexp-groups/5-parse-expression/solution.md b/9-regular-expressions/09-regexp-groups/5-parse-expression/solution.md index 3db5f667..7707edb0 100644 --- a/9-regular-expressions/09-regexp-groups/5-parse-expression/solution.md +++ b/9-regular-expressions/09-regexp-groups/5-parse-expression/solution.md @@ -1,8 +1,10 @@ A regexp for a number is: `pattern:-?\d+(\.\d+)?`. We created it in previous tasks. -An operator is `pattern:[-+*/]`. We put the dash `pattern:-` first, because in the middle it would mean a character range, we don't need that. +An operator is `pattern:[-+*/]`. -Note that a slash should be escaped inside a JavaScript regexp `pattern:/.../`. +Please note: +- Here the dash `pattern:-` goes first in the brackets, because in the middle it would mean a character range, while we just want a character `-`. +- A slash `/` should be escaped inside a JavaScript regexp `pattern:/.../`, we'll do that later. We need a number, an operator, and then another number. And optional spaces between them. diff --git a/9-regular-expressions/09-regexp-groups/article.md b/9-regular-expressions/09-regexp-groups/article.md index 6b2a17f1..9064ab71 100644 --- a/9-regular-expressions/09-regexp-groups/article.md +++ b/9-regular-expressions/09-regexp-groups/article.md @@ -47,13 +47,13 @@ In this example parentheses were used to make a group for repeating `pattern:(.. ## Contents of parentheses -Parentheses are numbered from left to right. The search engine remembers the content of each and allows to reference it in the pattern or in the replacement string. +Parentheses are numbered from left to right. The search engine remembers the content matched by each of them and allows to reference it in the pattern or in the replacement string. For instance, we'd like to find HTML tags `pattern:<.*?>`, and process them. Let's wrap the inner content into parentheses, like this: `pattern:<(.*?)>`. -We'll get them into an array: +We'll get both the tag as a whole and its content as an array: ```js run let str = '

Hello, world!

'; @@ -62,7 +62,7 @@ let reg = /<(.*?)>/; alert( str.match(reg) ); // Array: ["

", "h1"] ``` -The call to [String#match](mdn:js/String/match) returns groups only if the regexp has no `pattern:/.../g` flag. +The call to [String#match](mdn:js/String/match) returns groups only if the regexp only looks for the first match, that is: has no `pattern:/.../g` flag. If we need all matches with their groups then we can use `.matchAll` or `regexp.exec` as described in : @@ -162,9 +162,9 @@ alert(groups.day); // 30 As you can see, the groups reside in the `.groups` property of the match. -We can also use them in replacements, as `pattern:$` (like `$1..9`, but name instead of a digit). +We can also use them in the replacement string, as `pattern:$` (like `$1..9`, but a name instead of a digit). -For instance, let's rearrange the date into `day.month.year`: +For instance, let's reformat the date into `day.month.year`: ```js run let dateRegexp = /(?[0-9]{4})-(?[0-9]{2})-(?[0-9]{2})/; @@ -176,7 +176,7 @@ let rearranged = str.replace(dateRegexp, '$.$.$'); alert(rearranged); // 30.04.2019 ``` -If we use a function, then named `groups` object is always the last argument: +If we use a function for the replacement, then named `groups` object is always the last argument: ```js run let dateRegexp = /(?[0-9]{4})-(?[0-9]{2})-(?[0-9]{2})/; @@ -231,7 +231,12 @@ alert( result[1] ); // John ## Summary -- Parentheses can be: - - capturing `(...)`, ordered left-to-right, accessible by number. - - named capturing `(?...)`, accessible by name. - - non-capturing `(?:...)`, used only to apply quantifier to the whole groups. +Parentheses group together a part of the regular expression, so that the quantifier applies to it as a whole. + +Parentheses groups are numbered left-to-right, and can optionally be named with `(?...)`. + +The content, matched by a group, can be referenced both in the replacement string as `$1`, `$2` etc, or by the name `$name` if named. + +So, parentheses groups are called "capturing groups", as they "capture" a part of the match. We get that part separately from the result. + +We can exclude the group from remembering (make in "non-capturing") by putting `?:` at the start: `(?:...)`, that's used if we'd like to apply a quantifier to the whole group, but don't need it in the result.