正则表达式匹配css文件中的id?

Ric*_*uez 0 regex

输入以下内容:

#element{ color: #333; }
#element2,
.element3,
.whatever{ background: red; }

.test,
.test2, .test3,
#test111,
.element5555, #fooooooter{ display: none; }

#element8 tr .bacon{ font-size: 10000em; }
Run Code Online (Sandbox Code Playgroud)

如何使用正则表达式来匹配这些:

#element  
#element2  
#element8
#test111
Run Code Online (Sandbox Code Playgroud)

并且不匹配这些:

#333
Run Code Online (Sandbox Code Playgroud)

到目前为止我尝试了更多的变化,但我想不出任何有用的东西。

这是我最新尝试的:

(#).*[{]
Run Code Online (Sandbox Code Playgroud)

但它似乎跳过了一些选择器。

Her*_*lme 5

#-?[_a-zA-Z]+[_a-zA-Z0-9-]*(?=[^}]*\{)

它显式匹配 CSS 选择器中的有效字符。CSS 类名/选择器中哪些字符有效?

http://regexr.com?36e6v

content: '{}'不要忘记像应用此正则表达式之前一样删除注释和引用。使用正则表达式可以匹配评论和引用。单独使用展开技能将其移除。(我认为展开非常复杂且效率低下,足以排除集成的可能性。)

{}如果块像 Sass 中那样嵌套,它确实会匹配颜色,更少