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|sa
Match an optionals
then matchc
or matchsa
)
Close group 1ss$
Matchss
at 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 eithera
orc
ss$
Matchss
at the end of the string(?<!\.ass)
Negative lookbehind, assert not.ass
to the left
Note that if you want a match only, you can also use a non capture group (?:...)
instead.