$ npm install identifier-regex
Regular expression for matching valid JavaScript identifiers
npm install identifier-regex
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
, andundefined
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.
Returns a RegExp
for matching valid JavaScript identifiers.
Type: object
Type: boolean
Default: true
Only match an exact string.
© 2010 - cnpmjs.org x YWFE | Home | YWFE