I'm using node to read a file's contents. I need to match a particular import statement where the file looks something like this.
I need to match only the line or lines containing the "foo-bar" package. I'm interested in getting the imports from that package.
import React, { Component } from 'react'
import { withRouter } from 'react-router-dom'
import { connect } from 'react-redux'
import { foo, bar, fooBar } from 'foo-bar'
import { anotherThing } from 'another-module'
It can also span multiple lines like this.
import React, { Component } from 'react'
import { withRouter } from 'react-router-dom'
import { connect } from 'react-redux'
import {
foo,
bar,
fooBar
} from 'foo-bar'
import { anotherThing } from 'another-module'
I've tried using this regex but it's matching all lines from "import" to the end of the line containing foo-bar. I only want to match the import to the end of the line or lines that end with "foo-bar". Is this possible using JavaScript regex? Also, if there's a better way other than using regex i'm open to other options as well
import([\s\S]*?)(?=foo-bar').*
Single line example: https://regex101.com/r/U1j2G6/1
Multi-line example: https://regex101.com/r/kcA7gd/1