1

I have the following regex (JS.match) pattern /@import ['"](.*)['"]/g allowing for the external files inclusion so e.g.

@import "/my/path" returns /my/path

all works as expected but I would like to enable commenting out with // and /* */

so if string contains //@import "/my/path" or /* @import "/my/path" */ then there should be NO match.

any ideas?

Marcin
  • 5,469
  • 15
  • 55
  • 69
  • possible duplicate of [Javascript: negative lookbehind equivalent?](http://stackoverflow.com/questions/641407/javascript-negative-lookbehind-equivalent) – Barmar Dec 24 '13 at 16:07
  • What about `(/*|//)` and `(*/)?` around your regex? – sjagr Dec 24 '13 at 16:09
  • Something like this: /(/*|//) @import ['"](.*)['"](*/)/g, but need to escape slashes. – Parkash Kumar Dec 24 '13 at 16:10
  • @Barmar it is not a duplicate since looking behind is one thing but it also needs to look forward due to closing `*/` – Marcin Dec 24 '13 at 16:12
  • @ParkashKumar your solution is wrong as it should NOT match when // or /* – Marcin Dec 24 '13 at 16:13
  • @Marcin Ohhh, my bad! I thought it should match when // or /* – Parkash Kumar Dec 24 '13 at 16:14
  • Do you have multi-line comments with `/* */`? If not, and if you just want to avoid matches, something like `/^\s*@import ['"](.*)['"]/g` might work. – psquared Dec 24 '13 at 16:27
  • Do you have to do it in one regexp? Why not first check for a comment, and skip the line, then check for `@import`. – Barmar Dec 24 '13 at 16:34

1 Answers1

1
(?![/*])[^/* ]@import ['"](.*?)['"]

Live demo

Update

(?![/*])[^/* ]@import ['"](.*?)['"](?![^/]*?\*\/)

Live demo

Update #2

Use below regex with m flag:

(?:(?![/*]])[^/* ]|^ *)@import ['"](.*?)['"](?![^*]*?\*\/)

Live demo

revo
  • 47,783
  • 14
  • 74
  • 117
  • Thanks @Revo, its almost there, the only thing it doesn't account for is commenting out multi lines with `/*\n\n*/` e.g. (can't add break line in comments but I hope you know what I mean) `/* @import "/my/path14" \n@import "/my/path14" */` – Marcin Jan 16 '14 at 19:28
  • @revo, almost there, just one more issue, when /* is followed by break line and then multiple imports, each on its own line, it seems to include all of them except the last one, here is an example - http://regex101.com/r/zP4cJ7 – Marcin Jan 17 '14 at 09:57