0

I have a REGEX from github which seems to work fine, however it won't handle commented lines.

$regex = '/(?:@import)(?:\\s)?(?:(?:(?:\\()(["\'])?(?:[^"\')]+)\\1(?:\\))|(["\'])(?:.+)\\2)(?:[A-Z\\s])*)+(?:;)/';

This REGEX will be tested on content like this:

$content =
'// Layout & components // this should fail

@import "variables", "mixins"; // this should validate
// @import "tools", "functions"; // this should fail

// @import "media/vendor/bootstrap/scss/root"; // this should fail
@import "media/vendor/bootstrap/scss/reboot"; // this should validate
@import "../../../media/vendor/bootstrap/scss/tools"; // this should validate

@import url("some.css"); // this should fail
// @import url("some.css"); // this should fail
//@import url("some.css"); // this should fail
// @import some; // this should fail
//@import some; // this should fail
//@import"some"; // this should fail
//@importsome; // this should fail

In the above example, the first line will be ignored because it doesn't have '@import' but the second line still passes (will not be ignored) because it's not a handled case.

I've setup a regex101 which doesn't work live, however it does work on my local environment.

What I need:

  • I'm thinking I should add something like (^\\n?!:(\/\/)) at the beginning but I don't know if that's going to work or it won't do something else.
  • This REGEX would work with cases like @import url("bar.css"); I would like to remove this case, make it only work with `@import "bar.scss".
  • Some suggest I should strip comments before applying this REGEX, I tested this preg_replace("/^\\/{2}.*|\\/\\*.*\\*\\/$/", '', $content) from here but it breaks my code, so I would like to know what you think it's best.

Appreciate any reply. Thank you.

9
  • Please provide a minimal reproducible example which includes sample input that contains some of the trickier occurrences, then show your exact desired output from the sample input. Commented Mar 9, 2021 at 7:08
  • I've added a link to regex101, it's all there. Commented Mar 9, 2021 at 7:10
  • Volunteers shouldn't have to link chase or rely on an external resource (which may die one day in the future) to fully understand your question. Commented Mar 9, 2021 at 7:13
  • What are you trying to do? The question includes no details of what exactly you're trying to match and/or substitute. It'll be loads more helpful if you added some sample inputs and the outputs you desire, along with the code you've used so far and what it's not doing. Commented Mar 9, 2021 at 7:16
  • I added a sample code. Please check. Commented Mar 9, 2021 at 7:18

1 Answer 1

1

I suggest to use

$regex = "/^\\s*@import\\s+(\"|').*\\1\\s*[^;]*;.*$/m";

(You can try it out here: https://regex101.com/r/3gou5o/2)

This does only match for a import rule without anything than whitespace before the line start and nothing but a semicolon after the import url. Note that this also does match invalid imports.

Examples:

// Layout & components                               -> does not match
// @import "media/vendor/bootstrap/scss/root";       -> does not match
@import url("media/vendor/bootstrap/scss/reboot");   -> does not match
@import url("media/vendor/bootstrap/scss/type");     -> does not match
@import "media/vendor/bootstrap/scss/images" screen; -> matches
@import "media/vendor/bootstrap/scss/containers";    -> matches
@import 'media/vendor/bootstrap/scss/components' ;   -> matches
@import "images", "screen";                          -> matches

@import "this/is"/invalid";                          -> matches
Sign up to request clarification or add additional context in comments.

2 Comments

I edited my answer. It machtes all the cases you posted.
I forgot to mention about cases like @import "../../../media/vendor/bootstrap/scss/root";, please check.

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.