DD
DevDash

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

InputResult
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"
end

php

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

Want API access + no ads? Pro coming soon.