I have the following regex: /\.([s]?[ac]ss)$/. The problem is, it matches .scss, .sass, .css, .ass. How would I make it not match .ass?
CodePudding user response:
Also this will match .scss, .sass and .css only, it is very readable and self-explanatory
/\.(sc|sa|c)ss$/
CodePudding user response:
You can use
\.(?!a)(s?[ac]ss)$
CodePudding user response:
In your pattern \.([s]?[ac]ss)$ you match .ass because the leading s optional and the character class [ac] can match both characters.
Instead you could use lookarounds assertions, or use an alternation | to allow only certain alternatives.
Some other variations could be:
\.(s?c|sa)ss$
\.Match a.(Capture group 1s?c|saMatch an optionalsthen matchcor matchsa
)Close group 1ss$Matchssat the end of the string
\.(s[ac]|c)ss$
A variation on the previous pattern, now matching sa or sc or c
If in your environment the lookbehind assertion is supported:
\.s?[ac]ss$(?<!\.ass)
\.s?Match a.and optionals[ac]Match eitheraorcss$Matchssat the end of the string(?<!\.ass)Negative lookbehind, assert not.assto the left
Note that if you want a match only, you can also use a non capture group (?:...) instead.

