Skip to content

Instantly share code, notes, and snippets.

@MohamedLamineAllal
Last active February 2, 2019 01:32
Show Gist options
  • Save MohamedLamineAllal/31e9665dac3913cf8a3fd901ed9bbe43 to your computer and use it in GitHub Desktop.
Save MohamedLamineAllal/31e9665dac3913cf8a3fd901ed9bbe43 to your computer and use it in GitHub Desktop.
es6 import regex match
//With grouping : (can check and play with at => https://regexr.com/47jkp)
/import\s+?(?:(?:([\w*\s{},]*)\s+from\s+?)|)(?:(?:"(.*?)")|(?:'(.*?)'))[\s]*?(?:;|$|)/g
group 1 : the string between import and from. (if there is) (it will be undefined if not)
group 2 and 3 : the path of the import [only one of the two, the other is undefined)
if group 2: then it mean "" are used
if group 3: then it mean '' are used [i did that so i support same quotation mark check]
//without grouping (https://regexr.com/47jlq)
/import\s+?(?:(?:(?:[\w*\s{},]*)\s+from\s+?)|)(?:(?:".*?")|(?:'.*?'))[\s]*?(?:;|$|)/g
Tested on:
===========
import videos from './videos/index.js'
export default (socket, context) => {
// dynamically importing all the socket.io handler (it's dynamic import that happen at run time)
import {
something
} from "./test/okbb"
const f = 2;
import test from 'obb'
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";
import "module-name" // comment no problem
import "module-name' // doesn't match -> the opening and closing quation mark are different
importing hya from 'ttt' // doesn't match
import fbsfrom '' // doesn't match
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment