Line anchorsLes ancres de ligne

Until now an expression described characters. Anchors describe something else: where in the line the pattern has to be.

  • /^Hello/ → “Hello” at the start of a line only;
  • /!$/ → an exclamation mark at the end of a line only.

Unlike characters and classes, an anchor consumes no character: it marks a position, between two characters or at the edge of the text. That is what lets it combine with everything else without lengthening the match: /^\d/ finds a digit, not “a digit plus the start of the line”.

ExerciseExercice 1 · lines starting with Hellolignes commençant par Hello basicbasique not startedà faire solvedréussi keep tryingà retravailler
Find "Hello" only where it opens a line.
Trouvez « Hello » uniquement quand il ouvre une ligne.
Test textTexte de test
Hello world
Hi there
Goodbye Hello
Hello regex
Saying Hello is different
Hello world
Salut le monde
Bonjour Hello
Hello regex
Dire Hello n'est pas pareil
Expected matches:Correspondances attendues : Hello Hello
Expected matches:Correspondances attendues : Hello Hello
//gmu
ExerciseExercice 2 · lines starting with a vowellignes commençant par une voyelle basicbasique not startedà faire solvedréussi keep tryingà retravailler
Find the first character of the lines that start with a vowel.
Trouvez le premier caractère des lignes qui commencent par une voyelle.
Test textTexte de test
About the weather
It rains tomorrow
Every day counts
The sun shines
On we go
Aujourd'hui il fait beau
Il pleut demain
Elle est partie
Le soleil brille
On verra bien
Expected matches:Correspondances attendues : A I E O
Expected matches:Correspondances attendues : A I E O
//gmu
ExerciseExercice 3 · punctuation at the end of a lineponctuation en fin de ligne basicbasique not startedà faire solvedréussi keep tryingà retravailler
Find the punctuation marks (. ! ?) at the end of a line.
Trouvez les signes de ponctuation (. ! ?) en fin de ligne.
Test textTexte de test
Hello!
How are you?
Very well thanks! I look forward to seeing you soon
That is great.
See you
Bonjour!
Comment allez-vous?
Très bien merci! J'ai hâte de te voir prochainement
C'est génial.
À bientôt
Expected matches:Correspondances attendues : ! ? .
Expected matches:Correspondances attendues : ! ? .
//gmu

An empty line

An empty line is a line that starts and ends in the same place: /^$/, the two anchors with nothing between them. The first idea is often ^\n, but that one matches the \n character itself: the line break line breaksaut de ligne text encodingencodage du texte The character that ends a line in a file, stored in it like any other and invisible on screen. It is what divides a file into lines, so a file's line count is fixed : two of them make three lines, whatever width the window is. Windows writes it as two characters, a carriage return followed by a line feed (CR LF), where macOS and Linux use one (LF).Caractère qui termine une ligne dans un fichier, enregistré comme n'importe quel autre et invisible à l'écran. C'est lui qui découpe le fichier en lignes, si bien que leur nombre est fixe : deux sauts de ligne font trois lignes, quelle que soit la largeur de la fenêtre. Windows l'écrit au moyen de deux caractères, un retour chariot suivi d'un saut de ligne (CR LF), là où macOS et Linux n'en emploient qu'un (LF). separates lines, it is not part of one. And a line containing only spaces is not empty, which is exactly what you are trying to spot when you clean up a file.

ExerciseExercice 4 · empty lineslignes vides intermediateintermédiaire not startedà faire solvedréussi keep tryingà retravailler
Find the empty lines (no character at all). A frequent first idea is ^\n. But that matches the \n character itself, not an empty line: the line break separates lines, it is not part of one. An empty line is a line that starts and ends immediately. Careful: a line containing only spaces is not empty.
Trouvez les lignes vides (aucun caractère). Première intuition fréquente : ^\n. Mais cela correspond au caractère \n lui-même, pas à une ligne vide : le retour à la ligne sépare les lignes, il n'en fait pas partie. Une ligne vide est une ligne qui commence et finit immédiatement. Attention : une ligne qui ne contient que des espaces n'est pas vide.
Test textTexte de test
First line

Third line
   
Fifth line

Seventh line
Première ligne

Troisième ligne
   
Cinquième ligne

Septième ligne
Expected matches:Correspondances attendues : ␣ space · ⇥ tab · ⏎ line break · ◆ empty match␣ espace · ⇥ tabulation · ⏎ retour à la ligne · ◆ correspondance vide
Expected matches:Correspondances attendues : ␣ space · ⇥ tab · ⏎ line break · ◆ empty match␣ espace · ⇥ tabulation · ⏎ retour à la ligne · ◆ correspondance vide
//gmu
Written alternately in EN and FR, translation partly generated in both directions. Rédigé en alternance en EN et en FR, traduction en partie générée dans les deux sens. Updated 2026-08-27

Jusqu’ici, une expression décrivait des caractères. Les ancres décrivent autre chose : , dans la ligne, le motif doit se trouver.

  • /^Bonjour/ → « Bonjour » en début de ligne seulement ;
  • /!$/ → un point d’exclamation en fin de ligne seulement.

Contrairement aux caractères et aux classes, une ancre ne consomme aucun caractère : elle marque une position, entre deux caractères ou au bord du texte. C’est ce qui la rend combinable avec tout le reste sans allonger la correspondance : /^\d/ trouve un chiffre, pas « un chiffre et le début de la ligne ».

ExerciseExercice 1 · lines starting with Hellolignes commençant par Hello basicbasique not startedà faire solvedréussi keep tryingà retravailler
Find "Hello" only where it opens a line.
Trouvez « Hello » uniquement quand il ouvre une ligne.
Test textTexte de test
Hello world
Hi there
Goodbye Hello
Hello regex
Saying Hello is different
Hello world
Salut le monde
Bonjour Hello
Hello regex
Dire Hello n'est pas pareil
Expected matches:Correspondances attendues : Hello Hello
Expected matches:Correspondances attendues : Hello Hello
//gmu
ExerciseExercice 2 · lines starting with a vowellignes commençant par une voyelle basicbasique not startedà faire solvedréussi keep tryingà retravailler
Find the first character of the lines that start with a vowel.
Trouvez le premier caractère des lignes qui commencent par une voyelle.
Test textTexte de test
About the weather
It rains tomorrow
Every day counts
The sun shines
On we go
Aujourd'hui il fait beau
Il pleut demain
Elle est partie
Le soleil brille
On verra bien
Expected matches:Correspondances attendues : A I E O
Expected matches:Correspondances attendues : A I E O
//gmu
ExerciseExercice 3 · punctuation at the end of a lineponctuation en fin de ligne basicbasique not startedà faire solvedréussi keep tryingà retravailler
Find the punctuation marks (. ! ?) at the end of a line.
Trouvez les signes de ponctuation (. ! ?) en fin de ligne.
Test textTexte de test
Hello!
How are you?
Very well thanks! I look forward to seeing you soon
That is great.
See you
Bonjour!
Comment allez-vous?
Très bien merci! J'ai hâte de te voir prochainement
C'est génial.
À bientôt
Expected matches:Correspondances attendues : ! ? .
Expected matches:Correspondances attendues : ! ? .
//gmu

Une ligne vide

Une ligne vide est une ligne qui commence et finit au même endroit : /^$/, les deux ancres sans rien entre elles. La première idée est souvent ^\n, mais celle-là correspond au caractère \n lui-même : le saut de ligne line breaksaut de ligne text encodingencodage du texte The character that ends a line in a file, stored in it like any other and invisible on screen. It is what divides a file into lines, so a file's line count is fixed : two of them make three lines, whatever width the window is. Windows writes it as two characters, a carriage return followed by a line feed (CR LF), where macOS and Linux use one (LF).Caractère qui termine une ligne dans un fichier, enregistré comme n'importe quel autre et invisible à l'écran. C'est lui qui découpe le fichier en lignes, si bien que leur nombre est fixe : deux sauts de ligne font trois lignes, quelle que soit la largeur de la fenêtre. Windows l'écrit au moyen de deux caractères, un retour chariot suivi d'un saut de ligne (CR LF), là où macOS et Linux n'en emploient qu'un (LF). sépare les lignes, il n’en fait pas partie. Et une ligne qui ne contient que des espaces n’est pas vide, ce qui est précisément ce qu’on cherche à repérer quand on nettoie un fichier.

ExerciseExercice 4 · empty lineslignes vides intermediateintermédiaire not startedà faire solvedréussi keep tryingà retravailler
Find the empty lines (no character at all). A frequent first idea is ^\n. But that matches the \n character itself, not an empty line: the line break separates lines, it is not part of one. An empty line is a line that starts and ends immediately. Careful: a line containing only spaces is not empty.
Trouvez les lignes vides (aucun caractère). Première intuition fréquente : ^\n. Mais cela correspond au caractère \n lui-même, pas à une ligne vide : le retour à la ligne sépare les lignes, il n'en fait pas partie. Une ligne vide est une ligne qui commence et finit immédiatement. Attention : une ligne qui ne contient que des espaces n'est pas vide.
Test textTexte de test
First line

Third line
   
Fifth line

Seventh line
Première ligne

Troisième ligne
   
Cinquième ligne

Septième ligne
Expected matches:Correspondances attendues : ␣ space · ⇥ tab · ⏎ line break · ◆ empty match␣ espace · ⇥ tabulation · ⏎ retour à la ligne · ◆ correspondance vide
Expected matches:Correspondances attendues : ␣ space · ⇥ tab · ⏎ line break · ◆ empty match␣ espace · ⇥ tabulation · ⏎ retour à la ligne · ◆ correspondance vide
//gmu
Written alternately in EN and FR, translation partly generated in both directions. Rédigé en alternance en EN et en FR, traduction en partie générée dans les deux sens. Mise à jour 2026-08-30

Sources

Sources

Spotted an error? Une erreur ?

Language, content, a broken link: tell me what you saw and I will fix it. Langue, contenu, lien brisé : dites-moi ce que vous avez vu et je corrige.

Sending opens your mail app with the report addressed to me. L'envoi ouvre votre application de courriel avec le signalement déjà adressé à moi.