Skip to content

Translate lookahead-lookbehind tasks #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

The regexp for an integer number is `pattern:\d+`.
L'expression régulière pour un nombre entier est `pattern:\d+`.

We can exclude negatives by prepending it with the negative lookbehind: `pattern:(?<!-)\d+`.
Nous pouvons exclure les négatifs en les faisant précéder du lookbehind négatif : `pattern:(?<!-)\d+`.

Although, if we try it now, we may notice one more "extra" result:
Bien que, si nous l'essayons maintenant, nous remarquerons peut-être un autre résultat "supplémentaire":

```js run
let regexp = /(?<!-)\d+/g;
Expand All @@ -13,11 +13,11 @@ let str = "0 12 -5 123 -18";
console.log( str.match(regexp) ); // 0, 12, 123, *!*8*/!*
```

As you can see, it matches `match:8`, from `subject:-18`. To exclude it, we need to ensure that the regexp starts matching a number not from the middle of another (non-matching) number.
Comme vous pouvez le voir, il correspond à `match:8`, à partir de `subject:-18`. Pour l'exclure, nous devons nous assurer que l'expression régulière commence à correspondre à un nombre qui ne se trouve pas au milieu d'un autre nombre (non correspondant).

We can do it by specifying another negative lookbehind: `pattern:(?<!-)(?<!\d)\d+`. Now `pattern:(?<!\d)` ensures that a match does not start after another digit, just what we need.
Nous pouvons le faire en spécifiant un autre lookbehind négatif : `pattern:(?<!-)(?<!\d)\d+`. Maintenant, `pattern:(?<!\d)` garantit qu'une correspondance ne commence pas après un autre chiffre, juste ce dont nous avons besoin.

We can also join them into a single lookbehind here:
Nous pouvons également les joindre en un seul lookbehind ici:

```js run
let regexp = /(?<![-\d])\d+/g;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Find non-negative integers
# Trouver des nombres entiers non négatifs

There's a string of integer numbers.
Il y a une chaîne de nombres entiers.

Create a regexp that looks for only non-negative ones (zero is allowed).
Créez une expression régulière qui ne recherche que les expressions non négatives (zéro est autorisé).

An example of use:
Un exemple d'utilisation :
```js
let regexp = /your regexp/g;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
In order to insert after the `<body>` tag, we must first find it. We can use the regular expression pattern `pattern:<body.*?>` for that.
Pour insérer après la balise `<body>`, nous devons d'abord la trouver. Nous pouvons utiliser le modèle d'expression régulière `pattern:<body.*?>` pour cela.

In this task we don't need to modify the `<body>` tag. We only need to add the text after it.
Dans cette tâche, nous n'avons pas besoin de modifier la balise `<body>`. Nous n'avons qu'à ajouter le texte après.

Here's how we can do it:
Voici comment nous pouvons le faire :

```js run
let str = '...<body style="...">...';
Expand All @@ -11,9 +11,9 @@ str = str.replace(/<body.*?>/, '$&<h1>Hello</h1>');
alert(str); // ...<body style="..."><h1>Hello</h1>...
```

In the replacement string `$&` means the match itself, that is, the part of the source text that corresponds to `pattern:<body.*?>`. It gets replaced by itself plus `<h1>Hello</h1>`.
Dans la chaîne de remplacement, `$&` signifie la correspondance elle-même, c'est-à-dire la partie du texte source qui correspond à `pattern:<body.*?>`. Il est remplacé par lui-même suivi de `<h1>Hello</h1>`.

An alternative is to use lookbehind:
Une alternative consiste à utiliser lookbehind :

```js run
let str = '...<body style="...">...';
Expand All @@ -22,15 +22,15 @@ str = str.replace(/(?<=<body.*?>)/, `<h1>Hello</h1>`);
alert(str); // ...<body style="..."><h1>Hello</h1>...
```

As you can see, there's only lookbehind part in this regexp.
Comme vous pouvez le voir, il n'y a qu'une partie lookbehind dans cette expression régulière.

It works like this:
- At every position in the text.
- Check if it's preceeded by `pattern:<body.*?>`.
- If it's so then we have the match.
Cela fonctionne comme ceci :
- À chaque position dans le texte.
- Vérifiez s'il est précédé de `pattern:<body.*?>`.
- Si c'est le cas, nous avons le match.

The tag `pattern:<body.*?>` won't be returned. The result of this regexp is literally an empty string, but it matches only at positions preceeded by `pattern:<body.*?>`.
La balise `pattern:<body.*?>` ne sera pas renvoyée. Le résultat de cette expression régulière est littéralement une chaîne vide, mais elle ne correspond qu'aux positions précédées de `pattern:<body.*?>`.

So it replaces the "empty line", preceeded by `pattern:<body.*?>`, with `<h1>Hello</h1>`. That's the insertion after `<body>`.
Il remplace donc la "ligne vide", précédée de `pattern:<body.*?>`, par `<h1>Hello</h1>`. C'est l'insertion après `<body>`.

P.S. Regexp flags, such as `pattern:s` and `pattern:i` can also be useful: `pattern:/<body.*?>/si`. The `pattern:s` flag makes the dot `pattern:.` match a newline character, and `pattern:i` flag makes `pattern:<body>` also match `match:<BODY>` case-insensitively.
PS Les drapeaux d'expression régulière, tels que `pattern:s` et `pattern:i` peuvent également être utiles : `pattern:/<body.*?>/si`. Le drapeau `pattern:s` fait correspondre le point `pattern:.` à un caractère de retour à la ligne, et le drapeau `pattern:i` fait que `pattern:<body>` correspond également à `match:<BODY>` insensible à la casse.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Insert After Head
# Insérer après Body

We have a string with an HTML Document.
Nous avons une chaîne avec un document HTML.

Write a regular expression that inserts `<h1>Hello</h1>` immediately after `<body>` tag. The tag may have attributes.
Écrivez une expression régulière qui insère `<h1>Hello</h1>` immédiatement après la balise `<body>`. La balise peut avoir des attributs.

For instance:
Par exemple:

```js
let regexp = /your regular expression/;
Expand All @@ -20,7 +20,7 @@ let str = `
str = str.replace(regexp, `<h1>Hello</h1>`);
```

After that the value of `str` should be:
Après cela, la valeur de `str` devrait être :
```html
<html>
<body style="height: 200px"><h1>Hello</h1>
Expand Down