Regular Expressions 101

Community Patterns

JS/TS imports of external dependencies

1

Regular Expression
Rust

r#"
(?:(?:import|export)[^`'"]*from\s+[`'"])(?<from>[^`'"./][^`'"]*)(?:[`'"])|(?:\bimport)(?:\s+|\s*\(\s*)(?:[`'"])(?<plain>[^`'"./][^`'"]*)(?:[`'"])
"#
gm

Description

This regex can be used on a JavaScript/TypeScript file to find imports from third-party packages. This is useful for determining what to put in dependencies/peerDependencies. This is a Rust regex, which makes it easy to use with rg (ripgrep). Pro tip: combine rg with sort -u to get a list of all packages for your JS/TS project!

Based on a similar PCE2 regex

Submitted by Nikita Karamov - 3 days ago