Regex to Match JavaScript Import Statements
Match JavaScript and TypeScript ES6 import statements including named, default, and namespace imports. Captures module specifiers. Free regex tester.
Regular Expression
/import\s+(?:(?:\{[^}]*\}|\*\s+as\s+\w+|\w+)\s*(?:,\s*(?:\{[^}]*\}|\*\s+as\s+\w+|\w+)\s*)*from\s+)?['"][^'"]+['"]/gm
Token Breakdown
| Token | Description |
|---|---|
| i | Matches the literal character 'i' |
| m | Matches the literal character 'm' |
| p | Matches the literal character 'p' |
| o | Matches the literal character 'o' |
| r | Matches the literal character 'r' |
| t | Matches the literal character 't' |
| \s | Matches any whitespace character (space, tab, newline) |
| + | Matches the preceding element one or more times (greedy) |
| (?: | Start of non-capturing group |
| (?: | Start of non-capturing group |
| \{ | Matches a literal opening brace |
| [^}] | Negated character class — matches any character NOT in } |
| * | Matches the preceding element zero or more times (greedy) |
| \} | Matches a literal closing brace |
| | | Alternation — matches the expression before OR after the pipe |
| \* | Matches a literal asterisk |
| \s | Matches any whitespace character (space, tab, newline) |
| + | Matches the preceding element one or more times (greedy) |
| a | Matches the literal character 'a' |
| s | Matches the literal character 's' |
| \s | Matches any whitespace character (space, tab, newline) |
| + | Matches the preceding element one or more times (greedy) |
| \w | Matches any word character (letter, digit, underscore) |
| + | Matches the preceding element one or more times (greedy) |
| | | Alternation — matches the expression before OR after the pipe |
| \w | Matches any word character (letter, digit, underscore) |
| + | Matches the preceding element one or more times (greedy) |
| ) | End of group |
| \s | Matches any whitespace character (space, tab, newline) |
| * | Matches the preceding element zero or more times (greedy) |
| (?: | Start of non-capturing group |
| , | Matches the literal character ',' |
| \s | Matches any whitespace character (space, tab, newline) |
| * | Matches the preceding element zero or more times (greedy) |
| (?: | Start of non-capturing group |
| \{ | Matches a literal opening brace |
| [^}] | Negated character class — matches any character NOT in } |
| * | Matches the preceding element zero or more times (greedy) |
| \} | Matches a literal closing brace |
| | | Alternation — matches the expression before OR after the pipe |
| \* | Matches a literal asterisk |
| \s | Matches any whitespace character (space, tab, newline) |
| + | Matches the preceding element one or more times (greedy) |
| a | Matches the literal character 'a' |
| s | Matches the literal character 's' |
| \s | Matches any whitespace character (space, tab, newline) |
| + | Matches the preceding element one or more times (greedy) |
| \w | Matches any word character (letter, digit, underscore) |
| + | Matches the preceding element one or more times (greedy) |
| | | Alternation — matches the expression before OR after the pipe |
| \w | Matches any word character (letter, digit, underscore) |
| + | Matches the preceding element one or more times (greedy) |
| ) | End of group |
| \s | Matches any whitespace character (space, tab, newline) |
| * | Matches the preceding element zero or more times (greedy) |
| ) | End of group |
| * | Matches the preceding element zero or more times (greedy) |
| f | Matches the literal character 'f' |
| r | Matches the literal character 'r' |
| o | Matches the literal character 'o' |
| m | Matches the literal character 'm' |
| \s | Matches any whitespace character (space, tab, newline) |
| + | Matches the preceding element one or more times (greedy) |
| ) | End of group |
| ? | Makes the preceding element optional (zero or one times) |
| ['"] | Character class — matches any one of: '" |
| [^'"] | Negated character class — matches any character NOT in '" |
| + | Matches the preceding element one or more times (greedy) |
| ['"] | Character class — matches any one of: '" |
Detailed Explanation
This regex matches various forms of JavaScript ES6 import statements. Here is the token-by-token breakdown:
import — Matches the literal keyword import that begins every ES module import statement.
\s+ — Matches one or more whitespace characters after the import keyword.
(?: — Opens a non-capturing group for the optional import bindings section.
(?:{[^}]}|*\s+as\s+\w+|\w+) — A non-capturing group with three alternatives: named imports in curly braces like {useState, useEffect} matched by {[^}]}, namespace imports like * as utils matched by *\s+as\s+\w+, or default imports like React matched by \w+.
\s*(?:,\s*(?:{[^}]}|*\s+as\s+\w+|\w+)\s)* — Optionally matches additional import bindings separated by commas. This handles combinations like import React, { useState } from.
from\s+ — Matches the literal from keyword followed by whitespace, connecting the bindings to the module specifier.
)? — Makes the entire bindings section optional to support bare imports like import 'polyfill'.
['"][^'"]+['"] — Matches the module specifier string. It starts with a single or double quote, followed by one or more characters that are not quotes (the module path), and ends with a matching quote.
The g flag enables global matching and the m flag enables multiline mode. This pattern covers default imports, named imports, namespace imports, combined imports, and side-effect-only imports. It is useful for dependency analysis, bundler tools, and code refactoring.
Example Test Strings
| Input | Expected |
|---|---|
| import React from 'react' | Match |
| import { useState } from 'react' | Match |
| import 'polyfill' | Match |
| const x = require('module') | No Match |
| import * as utils from './utils' | Match |
Try It — Interactive Tester
Match Highlighting(4 matches)
Matches & Capture Groups
112 charsFlags: gmMatches: 4Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match JavaScript Function Declarations
/function\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\([^)]*\)/g
Regex to Match JavaScript Arrow Functions
/(?:(?:[a-zA-Z_$][a-zA-Z0-9_$]*)|\([^)]*\))\s*=>/g
Regex to Match Single-Line Comments
/\/\/.*$/gm
Regex to Match Programming Variable Names
/^[a-zA-Z_$][a-zA-Z0-9_$]*$/