Go (RE2)

npm Package Name in GO

Validate npm package names including scoped packages (@org/package), per the npm naming spec.

Try it in the GO tester →

Pattern

regexGO
^(?:@[a-z0-9\-*~][a-z0-9\-*._~]*\/)?[a-z0-9\-~][a-z0-9\-._~]*$

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`^(?:@[a-z0-9\-*~][a-z0-9\-*._~]*\/)?[a-z0-9\-~][a-z0-9\-._~]*$`)
	input := `react`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

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

react

Matches

  • react

Input

@types/node

Matches

  • @types/node

Input

MyPackage

No match

Same pattern, other engines

← Back to npm Package Name overview (all engines)