JavaScript/PHP/Java/Kotlin/Go/C/Swift/C# Comment Regular Expression

A regular expression to match C-like (/* … */ & //...) comments in JavaScript, PHP, Java, Kotlin, Go, C/C++, Swift, and C#.

/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm

Matches:

  • // comment
  • /* comment */

Non-matches:

  • <!– HTML comment –>
  • const a = b

See Also: