# comment-regex

> Regular expression for matching JavaScript comments

Latest version **2.0.0** (published 2021-10-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install comment-regex
pnpm add comment-regex
yarn add comment-regex
bun add comment-regex
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2021-10-13 |
| First published | 2014-09-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Node | ^12.20.0 \|\| ^14.13.1 \|\| >=16.0.0 |
| Dependencies | 0 |
| Unpacked size | 3.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 32 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | text, string, regex, regexp, match, test, find, pattern, comment, comments, js, javascript, line, block |

## Links

- npm: https://www.npmjs.com/package/comment-regex
- Repository: https://github.com/sindresorhus/comment-regex
- Homepage: https://github.com/sindresorhus/comment-regex#readme
- Issues: https://github.com/sindresorhus/comment-regex/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/comment-regex

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.0 (latest) — 2021-10-13
- 1.0.1 — 2018-03-23
- 1.0.0 — 2014-09-13

## README

# 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

```sh
npm install comment-regex
```

## Usage

```js
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.

---
_Source: https://npm.io/package/comment-regex · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
