Regex for JavaScript Import Statement
Regex Pattern
^import\s+(?:\{[^}]*\}|[a-zA-Z_$][\w$]*)\s+from\s+['"][^'"]+['"]ES module import statement in JavaScript/TypeScript
Quick Answer
The regex pattern for javascript import statement is `^import\s+(?:\{[^}]*\}|[a-zA-Z_$][\w$]*)\s+from\s+['"][^'"]+['"]`. ES module import statement in JavaScript/TypeScript. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| import React from 'react' | ✓ Matches |
| import { useState } from 'react' | ✓ Matches |
| import fs from 'fs' | ✓ Matches |
| require('react') | ✗ No match |
| import | ✗ No match |
| from "react" | ✗ No match |
Code Examples
javascript
const regex = /^import\s+(?:\{[^}]*\}|[a-zA-Z_$][\w$]*)\s+from\s+['"][^'"]+['"]/;
const isValid = regex.test(value);python
import re
pattern = r'^import\s+(?:\{[^}]*\}|[a-zA-Z_$][\w$]*)\s+from\s+['"][^'"]+['"]'
if re.match(pattern, value):
print("valid")ruby
pattern = /^import\s+(?:\{[^}]*\}|[a-zA-Z_$][\w$]*)\s+from\s+['"][^'"]+['"]/
if value =~ pattern
puts "valid"
endphp
if (preg_match('/^import\s+(?:\{[^}]*\}|[a-zA-Z_$][\w$]*)\s+from\s+['"][^'"]+['"]/', $value)) {
echo "valid";
}java
String pattern = "^import\\s+(?:\\{[^}]*\\}|[a-zA-Z_$][\\w$]*)\\s+from\\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)