# identifier-regex

> Regular expression for matching JavaScript identifiers

Latest version **1.1.0** (published 2026-07-05) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2026-07-05 |
| First published | 2024-05-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 1 |
| Unpacked size | 5.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 18 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | identifier, regex, regexp, regular, expression, keyword, word, reserved, javascript, ecmascript |

## Links

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

## Dependencies (1)

- [reserved-identifiers](https://npm.io/package/reserved-identifiers.md) ^1.0.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2026-07-05
- 1.0.1 — 2025-09-13
- 1.0.0 — 2024-05-05

## README

# identifier-regex

> Regular expression for matching valid [JavaScript identifiers](https://developer.mozilla.org/en-US/docs/Glossary/Identifier)

## Install

```sh
npm install identifier-regex
```

## Usage

```js
import identifierRegex from 'identifier-regex';

identifierRegex().test('foo');
//=> true

identifierRegex().test('1kg');
//=> false

identifierRegex().test('await'); // Reserved identifier
//=> false

'@x $x #x'.match(identifierRegex({exact: false}));
//=> ['$x']
```

> [!IMPORTANT]
> If you run the regex against untrusted user input in a server context, you should [give it a timeout](https://github.com/sindresorhus/super-regex). I do not consider ReDoS a valid vulnerability for this package.

> [!NOTE]
> Although `globalThis`, `Infinity`, `NaN`, and `undefined` are [properties of the global object](https://tc39.es/ecma262/#sec-value-properties-of-the-global-object) and not identifiers, they are not matched by the regex because they should generally not be used as identifiers.

## API

### identifierRegex(options?)

Returns a `RegExp` for matching valid JavaScript identifiers.

#### options

Type: `object`

##### exact

Type: `boolean`\
Default: `true`

Only match an exact string.

##### checkReserved

Type: `boolean`\
Default: `true`

Reject reserved words and global properties like `if`, `await`, and `undefined`.

These are valid identifier syntax but should not be used as variable names, so they are rejected by default. Disable this when you only care about the syntax, such as for property keys (`{if: 1}`) or dot notation (`foo.for`), where they are allowed.

```js
import identifierRegex from 'identifier-regex';

identifierRegex({checkReserved: false}).test('for');
//=> true
```

## Related

- [is-identifier](https://github.com/sindresorhus/is-identifier) - Check if a string is a valid JavaScript identifier

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