Code
C-style block comment Regex
Block comments that start with /* and end with */ appear in C, C++, Java, C#, JavaScript, Go, CSS and many other languages. This pattern finds each one with a lazy match, so it stops at the first closing */ and does not merge separate comments. It works across line breaks and also catches documentation comments that begin with /**. It is useful for stripping comments before minifying, extracting license headers or collecting JSDoc blocks. Like any regex over source code, it does not know about strings, so text such as "/* not a comment */" inside a string literal is matched too.
Pattern (JavaScript)
//\*[\s\S]*?\*//Matches the text "/*", then one of the characters \s\S, zero or more times, as few as possible (lazy), then the text "*/".
Examples
Matches
- ✓ /* note */
- ✓ int x; /* multi line */
- ✓ /** * JSDoc */
Does not match
- ✗ // line comment
- ✗ /* unclosed
- ✗ */ stray /*
- ✗ # shell comment
Try it
4 matches (JavaScript engine)
/* note */ int x; /* multi line */ /** * JSDoc */ // line comment /* unclosed */ stray /* # shell comment
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /\/\*[\s\S]*?\*\//g;
const text = "/* note */";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'/\*[\s\S]*?\*/')
text = '/* note */'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '#/\*[\s\S]*?\*/#';
$text = '/* note */';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("/\\*[\\s\\S]*?\\*/");
String text = "/* note */";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`/\*[\s\S]*?\*/`)
text := "/* note */"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"/\*[\s\S]*?\*/");
var text = "/* note */";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"/\*[\s\S]*?\*/").unwrap();
let text = "/* note */";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /\/\*[\s\S]*?\*\//
text = '/* note */'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Do block comments nest?
- Not in C, Java or JavaScript; the first */ always ends the comment. Some languages such as Rust and Swift allow nesting, which a single regex cannot track.
- Will it remove comment-like text inside strings?
- Yes, and that is a risk. A regex cannot tell whether /* appears inside a string literal. Use a tokenizer or the language's own parser when correctness matters.
- How do I match only JSDoc comments?
- Change the start to /\*\*(?!/) so the comment must begin with two asterisks. That selects documentation comments and skips ordinary block comments.