alex29
alex29

Reputation: 73

negative lookbehind regex followed by optional whitespaces

Can't make it work in Ruby: I need to process some CSS and replace } with !important } if it has no !important already. For this, my replacement looks like:

".a { zxcv: -90px !important; }".gsub(/(?<!!important;)\s*}/, ' !important;}')

which apparently should search for \s*} that is not preceded by !important;

It works only if I don't use \s* part, otherwise it ignores my negative check. I know that variable-length lookbehind isn't supported, but I don't use it here. How can \s* spoil my regex in this case?

Upvotes: 0

Views: 159

Answers (1)

anubhava
anubhava

Reputation: 785068

You can have another lookbehind like this for matching:

input.gsub(/(?<!!important; )(?<!!important;)\s*}/, ' !important;}')

Upvotes: 1

Related Questions