TL; DR; Consider using the right CSS parser, such as JSCSSP , for a final check.
It depends on the purpose, and the regex can be completely invalid.
If this is just an โattemptโ to see if it can โcontainโ CSS selectors, then I might be inclined to match too broadly, leading to a crash in something complicating CSS string values โโ(like โ}โ) or there are CSS comments, and will accept a wide range of input, which is invalid CSS:
(?:\s*\S+\s*{[^}]*})+
Similarly, an expression that should detect the simplest HTML (but invalid) with tags and only bad CSS cases (matches in comments or CSS lines or crazy child selectors):
<(?:br|p)[^>{]*>|</\w+\s*> // use case-insensitive
Happy coding.
Also see: CSS parsing in JavaScript / jQuery
user166390
source share