comment-regex

v2.0.0
Regular expression for matching JavaScript comments
text string regex regexp match test find pattern comment and 6 more...

comment-regex

Regular expression for matching JavaScript comments

This is pretty fragile and created for performance reasons where using a real parser would be overkill. Do not use it with untrusted user input.

Install

npm install comment-regex

Usage

import {commentRegex} from 'comment-regex';

// Contains a comment
commentRegex().test('/* unicorn */\nvar foo = true;');
//=> true

// Get the contents of a comment
commentRegex().exec('/* unicorn */\nvar foo = true;')[2].trim();

// Get all the comments
'/* unicorn */\nvar foo = true;\nvar unicorn = "rainbows"; // rainbow'.match(commentRegex());
//=> ['/* unicorn */', ' // rainbow']

API

The contents of the comment is in the first submatch.

commentRegex()

Returns a regex for matching line and block comments.

lineCommentRegex()

Returns a regex for matching line comments.

blockCommentRegex()

Returns a regex for matching block comments.

npm i comment-regex

Metadata

  • MIT
  • ^12.20.0 || ^14.13.1 || >=16.0.0
  • Sindre Sorhus
  • released 10/13/2021

Downloads

Maintainers