I’m trying to find all occurrences of a js import statement in a string of text
For example in this string:
import $ from "./components/jquery.js"; import dirtyFormUnload from "./components/dirtyFormUnload.js";
I’d like to match
import $ from "./components/jquery.js";
and
import dirtyFormUnload from "./components/dirtyFormUnload.js";
The regex I’ve put together is below, but it matches the entire string once, instead of each individual import statement
import\s+.*\s+from\s+['""].*\.js[^\s;]*[\s;]?
>Solution :
your regex statement is getting greedy, meaning that the first .* is matching as much as it can, including the next input statement. One way to fix this is to prevent it from matching ; characters like this:
import\s+[^;]*\s+from\s+['""][^;]*\.js[^\s;]*[\s;]?