Single-Line Comment (// or #)
Match single-line comments using either the `//` (C-family) or `#` (shell, Python, Ruby, YAML) marker.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("(?://|#).*$", "gm");
const input = "var x = 1; // assignment\\n# python style\\ny = 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).
Python (re) code
import re
pattern = re.compile(r"(?://|#).*$", re.MULTILINE)
input_text = "var x = 1; // assignment\n# python style\ny = 2"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?m)(?://|#).*$`)
input := `var x = 1; // assignment\n# python style\ny = 2`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
(?://|#).*$ (flags: gm)Raw source: (?://|#).*$
How it works
Examples
Input
var x = 1; // assignment\n# python style\ny = 2Matches
// assignment# python style
Input
Just a // sample lineMatches
// sample line
Input
no comments hereNo match
—Common use cases
- •Stripping line comments before evaluation
- •Linting rules for TODO/FIXME flagging
- •Documentation extraction (e.g. README from comments)
- •Cross-language source analysis
Related patterns
C-Style Block Comment
Text ProcessingMatch C-style /* ... */ block comments across multiple lines.
Trailing Whitespace (Per Line)
Text ProcessingMatch trailing spaces and tabs at the end of each line — the regex linters use to flag dirty whitespace.
JSON Log Line (Single-Line Object)
LogsMatch a single-line JSON object — typical of structured logging from services like slog, Bunyan, or Pino.
Python f-String Expression
Text ProcessingMatch `{expression}` placeholders inside Python f-strings (or any single-brace template syntax).