Regexdrill

Level 4: Extraction and cleanup Exercise 54 of 70 Free

Link text inside <a> tags

A regex exercise from Regexdrill. Solve it with a pattern box with live highlighting in your browser, checked against hidden test strings, and it is checked the moment you submit.

The task

Capture the text inside the first <a ...>...</a> element on the line in group 1. The opening tag may have attributes after a space, and the text can be empty. Other tags that start with a, like <abbr>, are not links.

What this exercise teaches

<a(?:\s[^>]*)?>(.*?)</a> only accepts <a> or <a followed by whitespace and attributes, so <abbr> cannot start a match. The lazy .*? stops at the first </a>, where a greedy .* would run to the last one on the line.

A hint if you get stuck

After <a, allow an optional part that starts with whitespace and runs to the >. Then use a lazy .*? for the text so it stops at the first </a>.

Try it now, free, no account needed

Every exercise on Regexdrill is free to try. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro adds timed mode and the certificate.

Open this exercise

More exercises in Extraction and cleanup