Regex for npm Package Name
Regex Pattern
^(?:@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$Valid npm package name (scoped or unscoped)
Quick Answer
The regex pattern for npm package name is `^(?:@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$`. Valid npm package name (scoped or unscoped). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| react | ✓ Matches |
| @types/node | ✓ Matches |
| my-package | ✓ Matches |
| @scope/my-lib | ✓ Matches |
| React | ✗ No match |
| MY_PACKAGE | ✗ No match |
| .hidden | ✗ No match |
| _private | ✗ No match |
Code Examples
javascript
const regex = /^(?:@[a-z0-9-~][a-z0-9-._~]*\\/)?[a-z0-9-~][a-z0-9-._~]*$/; const isValid = regex.test(value);
python
import re
pattern = r'^(?:@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^(?:@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/ if value =~ pattern puts "valid" end
php
if (preg_match('/^(?:@[a-z0-9-~][a-z0-9-._~]*\\/)?[a-z0-9-~][a-z0-9-._~]*$/', $value)) {
echo "valid";
}java
String pattern = "^(?:@[a-z0-9-~][a-z0-9-._~]*\\/)?[a-z0-9-~][a-z0-9-._~]*$"; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
JavaScript Variable Name
Valid JavaScript variable/identifier name
Python Variable Name
Valid Python identifier name
CSS Class Name
Valid CSS class name (starts with letter, underscore, or hyphen)
CSS ID Selector
CSS ID selector starting with #
JSON Key
JSON object key (quoted string followed by colon)
Semantic Version (Strict)
Strict semantic versioning (MAJOR.MINOR.PATCH)