如何基于正则表达式将css应用于div内容

时间:2012-10-11 07:18:33

标签: html css regex

如果div的innerHtml与正则表达式匹配,我想将css应用于我的div标签。我在某种程度上无法找到合适的搜索条件谷歌。如果已经在SO上询问过,请指出我。

//CSS:

#main div[/* regex here which matches Foo. It will make the div red */] {
    color: red;   
}

//HTML:
<div id="main">
    <div>This text contains Foo. It contains other stuff too</div>
    <div>Red color will not be applied to this div because regex doesn't match</div>
</div>

我见过使用正则表达式匹配属性的示例。例如this。我只是无法为正则表达式找到正确的“术语”以匹配div的内容。我不想使用Javascript并解析文本。

PS:我不必在IE,Safari,Opera上使用这个东西,所以Chrome / Mozilla的任何特定内容也会有所帮助。

感谢。

1 个答案:

答案 0 :(得分:0)

在CSS中找不到innerHTML中的内容是不可能的。但是可以在字符串中搜索HTML标记属性值。

例如:

a[href*='google.com'] {
  color: red;
}

您可以使用jQuery在javascript中完成此操作。 样本:http://www.jquery4u.com/syntax/jquery-basic-regex-selector-examples