{"id":11073,"library":"identifier-regex","title":"JavaScript Identifier Regular Expression","description":"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.","status":"active","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/sindresorhus/identifier-regex","tags":["javascript","identifier","regex","regexp","regular","expression","keyword","word","reserved","typescript"],"install":[{"cmd":"npm install identifier-regex","lang":"bash","label":"npm"},{"cmd":"yarn add identifier-regex","lang":"bash","label":"yarn"},{"cmd":"pnpm add identifier-regex","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"const identifierRegex = require('identifier-regex');","symbol":"identifierRegex","correct":"import identifierRegex from 'identifier-regex';"},{"note":"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.","wrong":"identifierRegex.test('foo');","symbol":"RegExp instance","correct":"const myIdentifierRegex = identifierRegex();"},{"note":"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.","wrong":"import { identifierRegex } from 'identifier-regex';","symbol":"identifierRegex with options","correct":"import identifierRegex from 'identifier-regex';\nconst looseRegex = identifierRegex({ exact: false });"}],"quickstart":{"code":"import identifierRegex from 'identifier-regex';\n\n// Basic usage: testing if a string is a valid identifier\nconsole.log(\"Is 'foo' a valid identifier?\", identifierRegex().test('foo'));\n//=> true\n\nconsole.log(\"Is '1kg' a valid identifier?\", identifierRegex().test('1kg'));\n//=> false (Identifiers cannot start with a number)\n\nconsole.log(\"Is 'await' a valid identifier?\", identifierRegex().test('await'));\n//=> false (Reserved word)\n\n// Using 'exact: false' to find identifiers within a larger string\nconst textWithIdentifiers = 'function _myVar_($param1) { const $var2 = 1; }';\nconst identifiersInText = textWithIdentifiers.match(identifierRegex({ exact: false, global: true }));\nconsole.log(`Identifiers found in \"${textWithIdentifiers}\":`, identifiersInText);\n//=> ['_myVar_', '$param1', '$var2']\n\n// Demonstrate that calling identifierRegex() returns a RegExp instance\nconst myRegexInstance = identifierRegex();\nconsole.log('Type of returned value:', typeof myRegexInstance);\nconsole.log('Is it a RegExp instance?', myRegexInstance instanceof RegExp);","lang":"typescript","description":"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."},"warnings":[{"fix":"Integrate with a regex timeout library like `super-regex` when processing untrusted input.","message":"When 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Pass `{ exact: false }` to the `identifierRegex` function when matching substrings: `identifierRegex({ exact: false }).test('foo bar')`.","message":"The 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 }`.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Call `identifierRegex()` to obtain the regular expression object: `identifierRegex().test('foo')`.","cause":"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.","error":"TypeError: identifierRegex is not a function"},{"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.","cause":"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).","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"Set the `exact` option to `false` when you want to find identifiers within a larger string: `identifierRegex({ exact: false }).test('foo bar')`.","cause":"The default `exact` option is `true`, meaning the regex tries to match the entire input string. `'foo bar'` is not a single, exact identifier.","error":"My regex isn't matching anything in a longer string (e.g., 'foo bar')."}],"ecosystem":"npm"}