Skip to content

Anchors: string start ^ and end $ #197

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 4 commits into from
May 9, 2020
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,5 +1,5 @@
An empty string is the only match: it starts and immediately finishes.
Une chaîne de caractères vide est la seule correspondance : elle commence et se termine aussitôt.

The task once again demonstrates that anchors are not characters, but tests.
Cette tâche montre à nouveau que les ancres ne sont pas des caractères, mais des tests.

The string is empty `""`. The engine first matches the `pattern:^` (input start), yes it's there, and then immediately the end `pattern:$`, it's here too. So there's a match.
La chaîne de caractères est vide `""`. Le moteur regarde en premier `pattern:^` (début de l'entrée), ça correspond, et immédiatement après la fin `pattern:$`, ça correspond également. Donc il y a une correspondance.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Regexp ^$

Which string matches the pattern `pattern:^$`?
Quelle chaîne de caractères correspond à `pattern:^$` ?
32 changes: 16 additions & 16 deletions 9-regular-expressions/04-regexp-anchors/article.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Anchors: string start ^ and end $
# Ancres : début ^ et fin $ d'une chaîne de caractères

The caret `pattern:^` and dollar `pattern:$` characters have special meaning in a regexp. They are called "anchors".
L'accent circonflexe `pattern:^` et le signe dollar `pattern:$` ont une signification particulière dans une regexp. Ils sont appelés "ancres".

The caret `pattern:^` matches at the beginning of the text, and the dollar `pattern:$` -- at the end.
L'accent circonflexe `pattern:^` correspond au début du texte, et le signe dollar `pattern:$` -- à la fin.

For instance, let's test if the text starts with `Mary`:
Par exemple, testons si le texte commence par `Mary`:

```js run
let str1 = "Mary had a little lamb";
alert( /^Mary/.test(str1) ); // true
```

The pattern `pattern:^Mary` means: "string start and then Mary".
Le paterne `pattern:^Mary` signifie : "le texte commence puis Mary".

Similar to this, we can test if the string ends with `snow` using `pattern:snow$`:
Similairement, nous pouvons vérifier si le texte termine par `snow` en utilisant `pattern:snow$`:

```js run
let str1 = "it's fleece was white as snow";
Expand All @@ -22,13 +22,13 @@ alert( /snow$/.test(str1) ); // true

In these particular cases we could use string methods `startsWith/endsWith` instead. Regular expressions should be used for more complex tests.

## Testing for a full match
## Test pour une correspondance complète

Both anchors together `pattern:^...$` are often used to test whether or not a string fully matches the pattern. For instance, to check if the user input is in the right format.
Les deux ancres mises ensemble `pattern:^...$` pour vérifier si une chaîne de caractères correspond entièrement à un paterne. Par exemple, pour vérifier si l'entrée de l'utilisateur est dans le bon format.

Let's check whether or not a string is a time in `12:34` format. That is: two digits, then a colon, and then another two digits.
Vérifions si une chaîne de caractères est une heure au format `12:34`. En résumé : deux nombres, puis deux points, et enfin deux autres nombres.

In regular expressions language that's `pattern:\d\d:\d\d`:
Dans le langage des expressions régulières, c'est `pattern:\d\d:\d\d`:

```js run
let goodInput = "12:34";
Expand All @@ -39,14 +39,14 @@ alert( regexp.test(goodInput) ); // true
alert( regexp.test(badInput) ); // false
```

Here the match for `pattern:\d\d:\d\d` must start exactly after the beginning of the text `pattern:^`, and the end `pattern:$` must immediately follow.
Ici, la correspondance pour `pattern:\d\d:\d\d` doit commencer juste après le début du texte `pattern:^`, et la fin `pattern:$` doit immédiatement suivre.

The whole string must be exactly in this format. If there's any deviation or an extra character, the result is `false`.
La chaîne entière doit être dans ce format. S'il y a la moindre déviation ou le moindre caractère de trop, le résultat sera `false`.

Anchors behave differently if flag `pattern:m` is present. We'll see that in the next article.
Les ancres agissent différemment si le marqueur `pattern:m` est présent. Nous verrons cela dans le prochain article.

```smart header="Anchors have \"zero width\""
Anchors `pattern:^` and `pattern:$` are tests. They have zero width.
```smart header="Les ancres n'ont \"aucune longueur\""
Les ancres `pattern:^` et `pattern:$` sont des tests. Elles n'ont aucune longueur.

In other words, they do not match a character, but rather force the regexp engine to check the condition (text start/end).
En d'autres termes, elles ne vérifient pas un caractère, mais forcent plutôt le moteur à vérifier une condition (le texte commence/termine).
```