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

TokenDescription
iMatches the literal character 'i'
mMatches the literal character 'm'
pMatches the literal character 'p'
oMatches the literal character 'o'
rMatches the literal character 'r'
tMatches the literal character 't'
\sMatches 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
\sMatches any whitespace character (space, tab, newline)
+Matches the preceding element one or more times (greedy)
aMatches the literal character 'a'
sMatches the literal character 's'
\sMatches any whitespace character (space, tab, newline)
+Matches the preceding element one or more times (greedy)
\wMatches any word character (letter, digit, underscore)
+Matches the preceding element one or more times (greedy)
|Alternation — matches the expression before OR after the pipe
\wMatches any word character (letter, digit, underscore)
+Matches the preceding element one or more times (greedy)
)End of group
\sMatches any whitespace character (space, tab, newline)
*Matches the preceding element zero or more times (greedy)
(?:Start of non-capturing group
,Matches the literal character ','
\sMatches 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
\sMatches any whitespace character (space, tab, newline)
+Matches the preceding element one or more times (greedy)
aMatches the literal character 'a'
sMatches the literal character 's'
\sMatches any whitespace character (space, tab, newline)
+Matches the preceding element one or more times (greedy)
\wMatches any word character (letter, digit, underscore)
+Matches the preceding element one or more times (greedy)
|Alternation — matches the expression before OR after the pipe
\wMatches any word character (letter, digit, underscore)
+Matches the preceding element one or more times (greedy)
)End of group
\sMatches 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)
fMatches the literal character 'f'
rMatches the literal character 'r'
oMatches the literal character 'o'
mMatches the literal character 'm'
\sMatches 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

InputExpected
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

//gm
gimsuy

Match Highlighting(4 matches)

import React from 'react' import { useState } from 'react' import 'polyfill' const x = require('module') import * as utils from './utils'

Matches & Capture Groups

#1import React from 'react'index 0
#2import { useState } from 'react'index 26
#3import 'polyfill'index 59
#4import * as utils from './utils'index 105
Pattern: 112 charsFlags: gmMatches: 4

Ctrl+Shift+C to copy regex

Customize this pattern →