identifier-regex
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

identifier-regex

Regular expression for matching valid JavaScript identifiers

Install

npm install identifier-regex

Usage

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. I do not consider ReDoS a valid vulnerability for this package.

[!NOTE] Although globalThis, Infinity, NaN, and undefined are 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.

Related

  • is-identifier - Check if a string is a valid JavaScript identifier

Package Sidebar

Install

npm i identifier-regex

Weekly Downloads

50,966

Version

1.0.0

License

MIT

Unpacked Size

4.38 kB

Total Files

5

Last publish

Collaborators

  • sindresorhus