JavaScript / ECMAScript

C-Style Block Comment in JS

Match C-style /* ... */ block comments across multiple lines.

Try it in the JS tester →

Pattern

regexJS
\/\*[\s\S]*?\*\/   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("\\/\\*[\\s\\S]*?\\*\\/", "g");
const input = "/* one */ var x = 1; /* two\\nlines */ y = 2;";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

\/\* matches the literal `/*`. [\s\S]*? lazily matches any characters including newlines (the [\s\S] idiom avoids needing a dotAll/s flag). \*\/ matches the closing `*/`. Lazy matching ensures adjacent comment blocks aren't merged into one giant match.

Examples

Input

/* one */ var x = 1; /* two\nlines */ y = 2;

Matches

  • /* one */
  • /* two\nlines */

Input

/** JSDoc here */

Matches

  • /** JSDoc here */

Input

// not a block comment

No match

Same pattern, other engines

← Back to C-Style Block Comment overview (all engines)