Last active
July 2, 2024 12:37
-
-
Save manekinekko/7e58a17bc62a9be47172 to your computer and use it in GitHub Desktop.
A regular Expression to parse ECMAScript6 import syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let regex = `import | |
(?: | |
["'\s]* | |
([\w*{}\n, ]+) | |
from\s* | |
)? | |
["'\s]* | |
([@\w/_-]+) | |
["'\s]* | |
;? | |
`; | |
// Matches... | |
/* | |
import { | |
Component | |
} from '@angular2/core'; | |
import defaultMember from "module-name"; | |
import * as name from "module-name "; | |
import { member } from " module-name"; | |
import { member as alias } from "module-name"; | |
import { member1 , member2 } from "module-name"; | |
import { member1 , member2 as alias2 , member3 as alias3 } from "module-name"; | |
import defaultMember, { member, member } from "module-name"; | |
import defaultMember, * as name from "module-name"; | |
import "module-name"; | |
*/ | |
thanks @ganeshkbhat, I used it for the reference. Mine particular case to find occurences of specific import package.
import(?:[\s.*]([\w*{}\n\r\t, ]+)[\s*]from)\s+['"]lodash['"];
^^^^^^
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I love it! Thanks for sharing!