npm Package Name in JS
Validate npm package names including scoped packages (@org/package), per the npm naming spec.
Try it in the JS tester →Pattern
regexJS
^(?:@[a-z0-9\-*~][a-z0-9\-*._~]*\/)?[a-z0-9\-~][a-z0-9\-._~]*$JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("^(?:@[a-z0-9\\-*~][a-z0-9\\-*._~]*\\/)?[a-z0-9\\-~][a-z0-9\\-._~]*$", "");
const input = "react";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
How the pattern works
The optional group (?:@[a-z0-9\-*~][a-z0-9\-*._~]*\/)? matches a @scope/ prefix for scoped packages. The main name [a-z0-9\-~][a-z0-9\-._~]* matches lowercase letters, digits, hyphens, dots, and tildes. Uppercase is not allowed per npm rules.
Examples
Input
reactMatches
react
Input
@types/nodeMatches
@types/node
Input
MyPackageNo match
—