More accurate package.json name regex #468

Closed
opened 2025-08-09 17:16:24 +00:00 by fergalmoran · 0 comments
Owner

Originally created by @shravan2x on 8/26/2017

As of VS2017, the package.json name field is required to match the regex ^[^A-Z]+$ and have a length not more than 214 characters.

image

I was unable to find the regex in this repo, so please redirect me if such an issue should be posted elsewhere.

I propose that this regex be changed to

^(?=.{1,214}$)(?:@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$

This regex follows the guidelines at https://docs.npmjs.com/files/package.json better. I have added some simple unit tests for it over at https://regex101.com/r/QgADad/3/tests.

Please let me know if you would like me to clarify the regex in any way. It is much more complex than the existing one, but also more accurate.

Ref:
https://stackoverflow.com/questions/695438/safe-characters-for-friendly-url
https://stackoverflow.com/questions/11197549/regular-expression-limit-string-size

EDIT 1: Improved regex
EDIT 2: Added more tests

*Originally created by @shravan2x on 8/26/2017* As of VS2017, the package.json `name` field is required to match the regex `^[^A-Z]+$` and have a length not more than 214 characters. ![image](https://user-images.githubusercontent.com/4096676/29739052-1769e680-89fa-11e7-8880-be8fde33423f.png) I was unable to find the regex in this repo, so please redirect me if such an issue should be posted elsewhere. I propose that this regex be changed to ```regex ^(?=.{1,214}$)(?:@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$ ``` This regex follows the guidelines at https://docs.npmjs.com/files/package.json better. I have added some simple unit tests for it over at https://regex101.com/r/QgADad/3/tests. Please let me know if you would like me to clarify the regex in any way. It is much more complex than the existing one, but also more accurate. **Ref**: https://stackoverflow.com/questions/695438/safe-characters-for-friendly-url https://stackoverflow.com/questions/11197549/regular-expression-limit-string-size **EDIT 1**: Improved regex **EDIT 2**: Added more tests
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/JavaScriptServices#468
No description provided.