Registry / testing / identifier-regex

identifier-regex

JSON →
library1.0.1jsnpmunverified

The `identifier-regex` package provides a precise regular expression function, `identifierRegex()`, designed to match valid JavaScript identifiers according to the ECMAScript specification. This includes proper handling of reserved words and specific syntax rules. The current stable version is 1.0.1, focusing on correctness and stability rather than rapid feature additions. Its primary differentiator is its strict adherence to JavaScript identifier rules, offering an `exact` option to control whether the regex matches the entire string or finds identifiers within a larger text. It explicitly excludes global properties like `Infinity` from being considered valid identifiers, aligning with common usage expectations. This library is ideal for parsers, linters, or any application needing to validate or extract JavaScript identifiers reliably.

npm install identifier-regex
INSTALL
IMPORT
SIG · IDENTIFIER-REGEX
I
identifier-regex
testingjavascriptv1.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

identifierRegex
import identifierRegex from 'identifier-regex';
const identifierRegex = require('identifier-regex');
This package is primarily designed for ES modules. While CommonJS might work with transpilation, direct `require` is not the recommended or native approach since Node.js >=18 engine requirement.
RegExp instance
const myIdentifierRegex = identifierRegex();
identifierRegex.test('foo');
The `identifierRegex` function *returns* a new `RegExp` instance each time it's called. You must call it to get the regex object to use `test()`, `match()`, etc.
identifierRegex with options
import identifierRegex from 'identifier-regex'; const looseRegex = identifierRegex({ exact: false });
import { identifierRegex } from 'identifier-regex';
The package exports a default function. Attempting a named import for `identifierRegex` will result in an error. Options are passed as an object to the function call.

Demonstrates importing the `identifierRegex` function, using it to test strings for identifier validity (including reserved words), and extracting identifiers from longer strings with the `exact: false` option, while also showing it returns a `RegExp` instance.

import identifierRegex from 'identifier-regex'; // Basic usage: testing if a string is a valid identifier console.log("Is 'foo' a valid identifier?", identifierRegex().test('foo')); //=> true console.log("Is '1kg' a valid identifier?", identifierRegex().test('1kg')); //=> false (Identifiers cannot start with a number) console.log("Is 'await' a valid identifier?", identifierRegex().test('await')); //=> false (Reserved word) // Using 'exact: false' to find identifiers within a larger string const textWithIdentifiers = 'function _myVar_($param1) { const $var2 = 1; }'; const identifiersInText = textWithIdentifiers.match(identifierRegex({ exact: false, global: true })); console.log(`Identifiers found in "${textWithIdentifiers}":`, identifiersInText); //=> ['_myVar_', '$param1', '$var2'] // Demonstrate that calling identifierRegex() returns a RegExp instance const myRegexInstance = identifierRegex(); console.log('Type of returned value:', typeof myRegexInstance); console.log('Is it a RegExp instance?', myRegexInstance instanceof RegExp);
Debug
Known issues
gotchaWhen running the regex against untrusted user input in a server context, it is strongly recommended to give the regex a timeout (e.g., using `super-regex`) to prevent potential Regular Expression Denial of Service (ReDoS) attacks. While the package maintainer does not consider ReDoS a direct vulnerability for this specific package, it's a critical security consideration for user-facing applications.
fix
Integrate with a regex timeout library like `super-regex` when processing untrusted input.
affects: >=1.0.0
gotchaThe regex will NOT match properties of the global object like `globalThis`, `Infinity`, `NaN`, and `undefined` as valid identifiers. While these are JavaScript built-ins, the library's design choice is to exclude them from identifier matching, which might surprise users expecting a broader match.
fix
If you need to match these global properties specifically, you will need to augment the result of `identifierRegex()` with additional patterns or handle them separately.
affects: >=1.0.0
gotchaThe default `exact` option for `identifierRegex()` is `true`. This means the regex will only match if the entire input string is a valid JavaScript identifier. If you intend to find or extract identifiers from within a larger string (e.g., `'foo bar'.match(identifierRegex())`), you must explicitly set `{ exact: false }`.
fix
Pass `{ exact: false }` to the `identifierRegex` function when matching substrings: `identifierRegex({ exact: false }).test('foo bar')`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: identifierRegex is not a function
Attempting to use the `identifierRegex` function directly as a `RegExp` object without calling it first (e.g., `identifierRegex.test('foo')`). The function returns a RegExp instance.
fix
Call `identifierRegex()` to obtain the regular expression object: `identifierRegex().test('foo')`.
SyntaxError: Cannot use import statement outside a module
Attempting to use ES module `import` syntax in a CommonJS (CJS) environment without proper configuration (e.g., without `'type': 'module'` in `package.json` for Node.js).
fix
Ensure your project is configured as an ES module by adding `'type': 'module'` to your `package.json`, or use a bundler that transpiles ES modules to CJS for older environments. This package requires Node.js >=18, which has robust ESM support.
My regex isn't matching anything in a longer string (e.g., 'foo bar').
The default `exact` option is `true`, meaning the regex tries to match the entire input string. `'foo bar'` is not a single, exact identifier.
fix
Set the `exact` option to `false` when you want to find identifiers within a larger string: `identifierRegex({ exact: false }).test('foo bar')`.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Amazon
1
Resources
identifier-regex — npm install identifier-regex · libregistry