【问题标题】:Match all CSS selectors that have a certain property and value with RegEx使用 RegEx 匹配所有具有特定属性和值的 CSS 选择器
【发布时间】:2017-06-14 01:19:09
【问题描述】:

我有一种感觉,我正在尝试做的事情真的很容易,但我现在已经坚持了一段时间,所以非常感谢你的一点帮助,这是我得到的一个例子:

.selector, .selector2 {
  color: #fff;  
  background: #f4c13b;
  opacity: 1;
}

.selector3:hover {
  color: red;
  opacity: 0;
  background: #f4c13b;
}

#selector4.class {
  background: #f4c13b;
  color: red;
}

@media (min-width:320px) {
  selector5 {
    opacity: 1;
    background: #f4c13b;
  }
}

现在,我想匹配每个具有 background: #f4c13b; 的选择器,因此捕获组将返回:.selector, .selector2.selector3:hover#selector4.classselector5

这是我目前得到的:/(.*) {[\S\s][^background]*background: #f4c13b;/gm

不幸的是,它只匹配将background: #f4c13b; 作为第一个属性的选择器,因此如果在{background: #f4c13b; 之前有某些内容,它将匹配:(

这是这背后的基本思想,每个选择器都在新行上,所以我想匹配并捕获{之前的所有内容,然后选择background: #f4c13b;之前的所有内容,然后选择}之前的所有内容,这样我就可以从第一个捕获组中提取选择器。

我正在使用 regexr.com 来测试表达式,我猜他们正在使用 JavaScript 风格的 RegEx。非常感谢您的帮助和解释,谢谢!

【问题讨论】:

  • 您是否需要专门使用正则表达式的解决方案?你的用例是什么?我认为完全解析 CSS 可能更容易,使用可以理解 CSS3 选择器甚至压缩 CSS 的东西,然后遍历所有选择器……
  • 我认为您需要告诉我们您使用的是哪种语言。在 Java(甚至 JavaScript)中,您会重复匹配一个模式并将选择器提取到某种数据结构中。
  • 顺便说一句,[^background] 并不意味着匹配除background 之外的任何字符串。相反,它表示匹配除b,a,c,k,g,r,o,u,n,d 之外的任何单个字符
  • 它不一定是 RegEx,我提出它是因为它是我所知道的唯一可以完成这项工作的工具。我正在使用 regexr.com 来提取 List 面板中的选择器,在该面板中输入$1,,这就是我获取所有选择器的方式,然后我将它们复制并粘贴到我需要的地方。感谢您的快速回复,非常感谢!
  • 您可以尝试使用这个:(.*)? {.*?background: #f4c13b; ...如果您使用的是 Notepad++ 之类的工具,您可以继续使用它。

标签: javascript regex regex-negation regex-lookarounds regex-greedy


【解决方案1】:

你可以使用这个正则表达式:/([.#]?[\w, .:]+?)(?= \{[^}]+?background: #f4c13b;[^}]+?\}\s*)/g

说明:

/                               : regex delimiter
    (                           : start group 1
        [.#]?                   : optional dot or hash
        [\w, .:]+?              : 1 or more alphanum, comma, space, dot, semicolumn, not greedy
    )                           : end group 1
    (?=                         : lookahead
        \{                      : open curly bracket
        [^}]+                   : 1 or more any char that is not close curly bracket
        background: #f4c13b;    : literally
        [^}]+?                  : 1 or more any char that is not close curly bracket, not greedy
        \}                      : close curly bracket
        \s*                     : 0 or more "spaces" including linebreak
    )                           : end lookahead
/g                              : regex delimiter, global flag

实际操作:

var x = `.selector, .selector2 {
  color: #fff;  
  background: #f4c13b;
  opacity: 1;
}

.selector3 {
  color: red;
  opacity: 0;
  background: #ffffff;
}

.selector3:hover {
  color: red;
  opacity: 0;
  background: #f4c13b;
}

#selector4 {
  color: red;
  opacity: 0;
  background: #f4c13b;
}

selector5 {
  color: red;
  opacity: 0;
  background: #f4c13b;
}
@media (min-width:320px) {
  selector6 {
    opacity: 1;
    background: #f4c13b;
  }
}`;
var r = x.match(/([.#]?[\w, .:]+?)(?= \{[^}]+?background: #f4c13b;[^}]+?\}\s*)/g);
console.log(r);

【讨论】:

  • 非常感谢这在大多数情况下都很好用,但我应该在问选择器有时可以以 #selector 开头甚至根本没有任何东西 div 的问题时提到过,所以这个代码将因 ID 和 html 元素选择器而失败。还有一件事,如果前面有@media { ...,这段代码也会失败,考虑到这些情况,是否有可能使这段代码更加防弹?再次感谢!
  • @pbe:查看我的编辑。它适用于.# 或什么都没有。对于@media,“之前”是什么意思?请用完整的测试用例编辑您的问题
  • @pbe:现在看来还可以。不是吗?
  • 太棒了!代表所有前端编码员非常感谢你:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 1970-01-01
  • 2012-06-08
  • 1970-01-01
  • 2011-08-03
  • 1970-01-01
相关资源
最近更新 更多