identifier-regex
Regular expression for matching JavaScript identifiers
Last updated 6 months ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install identifier-regex 
SYNC missed versions from official npm registry.

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

Current Tags

  • 1.0.0                                ...           latest (6 months ago)

1 Versions

  • 1.0.0                                ...           6 months ago
Maintainers (1)
Downloads
Total 1
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (2)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE