all jazz
all jazz

Reputation: 2017

matching ending CSS curly brace when it ends without a semicolon

How to match the ending CSS curly brace } that doesn't have semicolon in front of it? See examples in the following cases:

This should be matched:

.style { margin: 0px; width: 10px }
.style {
   margin: 0px;
   width: 10px
}

But not in this:

.style { margin: 0px; width: 10px; }
.style { margin: 0px; width: 10px;}
.style {
   margin: 0px;
   width: 10px;
}

I'm trying to use this, but looks like there are some issues with using negative match:

string.match(/[^;](!?\s+)?(})/mg)

Upvotes: 1

Views: 207

Answers (1)

Markus Jarderot
Markus Jarderot

Reputation: 89211

Assuming JavaScript. This adds the missing semicolons:

s.replace(/([^{};\s])(\s*})/g, "$1;$2")

I include {} in the first character class, since there could be empty rules:

.foo {}

The pattern captures the last non-whitespace character before the brace, since JavaScript does not support look-behinds. Since I was already capturing characters, I captured the closing brace as well, instead of using a look-ahead.

Upvotes: 1

Related Questions