Regex for package.json Script
Regex Pattern
"([a-zA-Z0-9:_-]+)"\s*:\s*"([^"]*)"package.json script entry ("name": "command")
Quick Answer
The regex pattern for package.json script is `"([a-zA-Z0-9:_-]+)"\s*:\s*"([^"]*)"`. package.json script entry ("name": "command"). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| "build": "next build" | ✓ Matches |
| "dev": "vite" | ✓ Matches |
| "test:unit": "vitest" | ✓ Matches |
| build: next build | ✗ No match |
| not json | ✗ No match |
| "key" | ✗ No match |
Code Examples
javascript
const regex = /"([a-zA-Z0-9:_-]+)"\s*:\s*"([^"]*)"/; const isValid = regex.test(value);
python
import re
pattern = r'"([a-zA-Z0-9:_-]+)"\s*:\s*"([^"]*)"'
if re.match(pattern, value):
print("valid")ruby
pattern = /"([a-zA-Z0-9:_-]+)"\s*:\s*"([^"]*)"/ if value =~ pattern puts "valid" end
php
if (preg_match('/"([a-zA-Z0-9:_-]+)"\s*:\s*"([^"]*)"/', $value)) {
echo "valid";
}java
String pattern = ""([a-zA-Z0-9:_-]+)"\\s*:\\s*"([^"]*)""; 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)