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?

8
  • possible duplicate of Javascript: negative lookbehind equivalent? Commented Dec 24, 2013 at 16:07
  • What about (/*|//) and (*/)? around your regex? Commented Dec 24, 2013 at 16:09
  • Something like this: /(/*|//) @import ['"](.*)['"](*/)/g, but need to escape slashes. Commented Dec 24, 2013 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 */ Commented Dec 24, 2013 at 16:12
  • @ParkashKumar your solution is wrong as it should NOT match when // or /* Commented Dec 24, 2013 at 16:13

1 Answer 1

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

Live demo

Update

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

Live demo

Update #2

Use below regex with m flag:

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

Live demo

Sign up to request clarification or add additional context in comments.

2 Comments

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" */
@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 - regex101.com/r/zP4cJ7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.