I'm reading a file using Node's fs.readFileSync(file)
. I need to find the index of the last occurrence of an import statement.
The js file that I'm reading will look something like:
import React from 'react';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { mount } from 'react-mounter';
import { AppLayout } from '../../ui/layouts/AppLayout';
import HomePage from '../../ui/pages/HomePage';
FlowRouter.route('/', {
name: 'home',
action() {
mount(AppLayout, {
content: (<HomePage />)
});
}
});
So in this particular case, I would need to find the index of the semi-colon for this import statement: import HomePage from '../../ui/pages/HomePage';
since it's the last one.
I've looked into str.lastIndexOf(searchValue[, fromIndex])
but it takes a string as the searchValue
, and in this case I need to pass in a regex.
It seems like I need a regex that will lookup in reverse.
How do I match and get the index of the last occurrence of import?