Regex to Match Multi-Line Block Comments

Match multi-line block comments delimited by /* and */ in JavaScript, C, Java, and similar languages. Handles nested newlines. Free regex tester.

Regular Expression

/\/\*[\s\S]*?\*\//g

Token Breakdown

TokenDescription
\/Matches a literal forward slash
\*Matches a literal asterisk
[\s\S]Character class — matches any one of: \s\S
*?Matches the preceding element zero or more times (lazy/non-greedy)
\*Matches a literal asterisk
\/Matches a literal forward slash

Detailed Explanation

This regex matches block comments (also called multi-line comments) that use the /* ... */ syntax. Here is the token-by-token breakdown:

/* — Matches the literal opening delimiter of a block comment: a forward slash followed by an asterisk. Both characters are escaped with backslashes. The forward slash is escaped by convention, and the asterisk is escaped because it is a regex quantifier metacharacter.

[\s\S]*? — Matches any character including newlines, using a lazy (non-greedy) quantifier. The character class [\s\S] combines whitespace (\s) and non-whitespace (\S) characters, effectively matching every possible character including newlines. This is necessary because the dot (.) does not match newline characters by default. The *? lazy quantifier ensures the match stops at the first closing delimiter rather than the last one.

*/ — Matches the literal closing delimiter: an asterisk followed by a forward slash. Both characters are escaped with backslashes.

The g flag enables global matching to find all block comments in the source code. This pattern correctly handles comments spanning multiple lines, single-line block comments, and empty block comments like /**/.

This pattern is essential for code parsing, syntax highlighting, minification, comment extraction, and documentation generation tools like JSDoc or Javadoc. It is used in build tools, linters, and IDE extensions. Note that this pattern does not handle nested block comments, which are not supported in most C-style languages anyway.

Example Test Strings

InputExpected
/* comment */Match
/* multi line comment */Match
// single lineNo Match
no commentNo Match
/** JSDoc comment */Match

Try It — Interactive Tester

//g
gimsuy

Match Highlighting(3 matches)

/* comment */ /* multi line comment */ // single line no comment /** JSDoc comment */

Matches & Capture Groups

#1/* comment */index 0
#2/* multi line comment */index 14
#3/** JSDoc comment */index 65
Pattern: 16 charsFlags: gMatches: 3

Ctrl+Shift+C to copy regex

Customize this pattern →