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
| Token | Description |
|---|---|
| \/ | 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
| Input | Expected |
|---|---|
| /* comment */ | Match |
| /* multi line comment */ | Match |
| // single line | No Match |
| no comment | No Match |
| /** JSDoc comment */ | Match |
Try It — Interactive Tester
Match Highlighting(3 matches)
Matches & Capture Groups
16 charsFlags: gMatches: 3Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match Single-Line Comments
/\/\/.*$/gm
Regex to Match HTML Comments
/<!--[\s\S]*?-->/g
Regex to Match JavaScript Function Declarations
/function\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\([^)]*\)/g
Regex to Match JavaScript Import Statements
/import\s+(?:(?:\{[^}]*\}|\*\s+as\s+\w+|\w+)\s*(?:,\s*(?:\{[^}]*\}|\*\s+as\s+\w+|\w+)\s*)*from\s+)?['"][^'"]+['"]/gm