Regexdrill

Level 5: Advanced Exercise 83 of 88 Free

The s flag: comments that span lines

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

Each test is a block of code. Capture the text inside every /* ... */ comment in group 1, without the comment markers. A comment can span several lines, so the dot has to match line breaks too: set the s flag. Each comment ends at the first */ after its start.

What this exercise teaches

Without the s flag the dot stops at a line break, so a comment that spans lines is never matched. The lazy .*? keeps two comments on one line apart; a greedy .* would run from the first /* to the last */.

A hint if you get stuck

Escape the stars of the markers: /\* and \*/. Between them use a lazy (.*?) so the match stops at the first closing marker, and add the s flag so the dot also matches a line break.

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. The worked solution opens once you solve it or after two tries.

Open this exercise

More exercises in Advanced