Skip to main content
Regexflux

JavaScript Import Statement

intermediate

Matches ES module import statements.

javascriptimportmoduledevelopment

Pattern

/import\s+(?:\{[^}]*\}|\*\s+as\s+\w+|\w+)(?:\s*,\s*(?:\{[^}]*\}|\*\s+as\s+\w+))?\s+from\s+['"][^'"]+['"]/g

Try It

0 of 6 strings matched

Explanation

Matches ES module import syntax: default imports, named imports in braces, namespace imports (* as), and combinations, followed by 'from' and a module specifier string.

Test Strings

Matching

  • import React from 'react'
  • import { useState, useEffect } from 'react'
  • import * as utils from './utils'

Non-matching

  • const x = require('module')
  • import 'styles.css'
  • // import from 'module'

Language Compatibility

LanguageSupport
JSFull support
PYTHONFull support
JAVAFull support
PHPFull support
GOFull support

Code Snippets

const regex = /import\s+(?:\{[^}]*\}|\*\s+as\s+\w+|\w+)(?:\s*,\s*(?:\{[^}]*\}|\*\s+as\s+\w+))?\s+from\s+['"][^'"]+['"]/g;
const text = "your text here";
const matches = text.match(regex);
console.log(matches);

Common Variations

Side-effect import

import\s+['"][^'"]+['"]

Matches import 'module' without bindings

Related Patterns