Code
ES module import statement Regex
ES modules load code with import statements in several forms: a default import, named imports in braces, a namespace import with * as, a mix of default and named, or a bare side-effect import of a path. This pattern recognises those single-line forms and requires the module path to be in single or double quotes, with an optional trailing semicolon. It is handy for quick dependency scans, codemods and editor snippets that list what a file imports. It does not understand import statements spread over several lines, dynamic import() calls or TypeScript type-only modifiers, so use a real parser for production refactoring.
Pattern (JavaScript)
/^\s*import\s+(?:[\w$]+(?:\s*,\s*)?)?(?:\{[^}]*\}|\*\s+as\s+[\w$]+)?\s*(?:from\s+)?["'][^"']+["'];?\s*$/Starts at the start of the line/string, then a whitespace character (space, tab, newline), zero or more times, then the text "import", then one or more whitespace characters, then (one of the characters \w$, one or more times, then (a whitespace character (space, tab, newline), zero or more times, then the character ",", then a whitespace character (space, tab, newline), zero or more times), optionally (zero or one time)), optionally (zero or one time), then (the character "{", then any character except }, zero or more times, then the character "}" OR the character "*", then one or more whitespace characters, then the text "as", then one or more whitespace characters, then one of the characters \w$, one or more times), optionally (zero or one time), then a whitespace character (space, tab, newline), zero or more times, then (the text "from", then one or more whitespace characters), optionally (zero or one time), then one of the characters "', then any character except "', one or more times, then one of the characters "', then the character ";", optionally (zero or one time), then a whitespace character (space, tab, newline), zero or more times, then the end of the line/string.
Examples
Matches
- ✓ import React from "react";
- ✓ import { useState, useEffect } from 'react'
- ✓ import * as path from "node:path";
- ✓ import "./styles.css";
Does not match
- ✗ const x = require('x');
- ✗ import { a } from;
- ✗ export { a } from './b';
- ✗ // import nothing
Try it
0 matches (JavaScript engine)
import React from "react";
import { useState, useEffect } from 'react'
import * as path from "node:path";
import "./styles.css";
const x = require('x');
import { a } from;
export { a } from './b';
// import nothingCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^\s*import\s+(?:[\w$]+(?:\s*,\s*)?)?(?:\{[^}]*\}|\*\s+as\s+[\w$]+)?\s*(?:from\s+)?["'][^"']+["'];?\s*$/g;
const text = "import React from \"react\";";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile('^\\s*import\\s+(?:[\\w$]+(?:\\s*,\\s*)?)?(?:\\{[^}]*\\}|\\*\\s+as\\s+[\\w$]+)?\\s*(?:from\\s+)?["\'][^"\']+["\'];?\\s*$')
text = 'import React from "react";'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^\s*import\s+(?:[\w$]+(?:\s*,\s*)?)?(?:\{[^}]*\}|\*\s+as\s+[\w$]+)?\s*(?:from\s+)?["\'][^"\']+["\'];?\s*$/';
$text = 'import React from "react";';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^\\s*import\\s+(?:[\\w$]+(?:\\s*,\\s*)?)?(?:\\{[^}]*\\}|\\*\\s+as\\s+[\\w$]+)?\\s*(?:from\\s+)?[\"'][^\"']+[\"'];?\\s*$");
String text = "import React from \"react\";";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^\s*import\s+(?:[\w$]+(?:\s*,\s*)?)?(?:\{[^}]*\}|\*\s+as\s+[\w$]+)?\s*(?:from\s+)?["'][^"']+["'];?\s*$`)
text := "import React from \"react\";"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^\s*import\s+(?:[\w$]+(?:\s*,\s*)?)?(?:\{[^}]*\}|\*\s+as\s+[\w$]+)?\s*(?:from\s+)?[""'][^""']+[""'];?\s*$");
var text = "import React from \"react\";";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r#"^\s*import\s+(?:[\w$]+(?:\s*,\s*)?)?(?:\{[^}]*\}|\*\s+as\s+[\w$]+)?\s*(?:from\s+)?["'][^"']+["'];?\s*$"#).unwrap();
let text = "import React from \"react\";";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^\s*import\s+(?:[\w$]+(?:\s*,\s*)?)?(?:\{[^}]*\}|\*\s+as\s+[\w$]+)?\s*(?:from\s+)?["'][^"']+["'];?\s*$/
text = 'import React from "react";'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I extract the module path?
- Wrap [^"']+ in a capture group and read it after a match. That gives you the specifier, such as react or ./styles.css, which is what dependency tools usually need.
- Why are multi-line imports not supported?
- Long named import lists are often split across lines, and handling that with a regex gets fragile quickly. A JavaScript parser such as the one in your bundler handles every form correctly.
- Does it match dynamic import() calls?
- No. Dynamic imports are expressions like await import('./x'), not statements, and can appear anywhere in code. Search for import\(\s*["'] separately if you need them.