This commit is contained in:
Ilya Kantor 2019-08-15 08:19:00 +03:00
parent 99cbc65d80
commit 7fd3eb1797
2 changed files with 10 additions and 8 deletions

View file

@ -12,11 +12,11 @@ For example, we can use a network request to:
...And all of that without reloading the page!
There's an umbrella term "AJAX" (abbreviated <b>A</b>synchronous <b>J</b>avaScript <b>A</b>nd <b>X</b>ML) for network requests from JavaScript. We don't have to use XML though: the term comes from old times, that's that word is there. You may have heard that term already.
There's an umbrella term "AJAX" (abbreviated <b>A</b>synchronous <b>J</b>avaScript <b>A</b>nd <b>X</b>ML) for network requests from JavaScript. We don't have to use XML though: the term comes from old times, that's why that word is there. You may have heard that term already.
There are multiple ways to send a network request and get information from the server.
The `fetch()` method is modern and versatile, so we'll start with it. It's not supported by old browsers (can be polyfilled), but very well supported among the new ones.
The `fetch()` method is modern and versatile, so we'll start with it. It's not supported by old browsers (can be polyfilled), but very well supported among the modern ones.
The basic syntax is:
@ -27,11 +27,13 @@ let promise = fetch(url, [options])
- **`url`** -- the URL to access.
- **`options`** -- optional parameters: method, headers etc.
Without `options`, that is a simple GET request, downloading the contents of the `url`.
The browser starts the request right away and returns a promise that the calling code should use to get the result.
Getting a response is usually a two-stage process.
**First, the `promise` resolves with an object of the built-in [Response](https://fetch.spec.whatwg.org/#response-class) class as soon as the server responds with headers.**
**First, the `promise`, returned by `fetch`, resolves with an object of the built-in [Response](https://fetch.spec.whatwg.org/#response-class) class as soon as the server responds with headers.**
At this stage we can check HTTP status, to see whether it is successful or not, check headers, but don't have the body yet.

View file

@ -43,7 +43,7 @@ let reg = /[-.\w]+@([\w-]+\.)+[\w-]{2,20}/g;
alert("my@mail.com @ his@site.com.uk".match(reg)); // my@mail.com, his@site.com.uk
```
In this example parentheses were used to make a group for repeating `pattern:(...)+`. But there are other uses too, let's see them.
In this example parentheses were used to make a group for repetitions `pattern:([\w-]+\.)+`. But there are other uses too, let's see them.
## Contents of parentheses
@ -53,7 +53,7 @@ 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 both the tag as a whole and its content as an array:
Then we'll get both the tag as a whole and its content:
```js run
let str = '<h1>Hello, world!</h1>';
@ -111,7 +111,7 @@ Then groups, numbered from left to right. Whichever opens first gives the first
Then in `result[2]` goes the group from the second opening `pattern:(` till the corresponding `pattern:)` -- tag name, then we don't group spaces, but group attributes for `result[3]`.
**If a group is optional and doesn't exist in the match, the corresponding `result` index is present (and equals `undefined`).**
**Even if a group is optional and doesn't exist in the match, the corresponding `result` array item is present (and equals `undefined`).**
For instance, let's consider the regexp `pattern:a(z)?(c)?`. It looks for `"a"` optionally followed by `"z"` optionally followed by `"c"`.
@ -208,7 +208,7 @@ let rearranged = str.replace(dateRegexp, (str, ...args) => {
## Non-capturing groups with ?:
Sometimes we need parentheses to correctly apply a quantifier, but we don't want the contents in results.
Sometimes we need parentheses to correctly apply a quantifier, but we don't want their contents in results.
A group may be excluded by adding `pattern:?:` in the beginning.