3

我正在调用indexOf中的头部jQuery。我想看看 CSS 是否包含任何font-weight:bold属性。问题是,该属性可以有 4 个更改:

  1. font-weight:bold
  2. font-weight: bold
  3. font-weight :bold
  4. font-weight : bold

如何使用正则表达式匹配这个?

4

5 回答 5

4

试试这个

\b(?:font-weight\s*:[^;\{\}]*?\bbold)\b

或者会更好地使用:

\b(?:font-weight[\s*\\]*:[\s*\\]*?\bbold\b)

解释

\b                # Assert position at a word boundary
(?:               # Match the regular expression below
   font-weight       # Match the characters “font-weight” literally
   [\s*\\]           # Match a single character present in the list below
                        # A whitespace character (spaces, tabs, and line breaks)
                        # The character “*”
                        # A \ character
      *                 # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   :                 # Match the character “:” literally
   [\s*\\]           # Match a single character present in the list below
                        # A whitespace character (spaces, tabs, and line breaks)
                        # The character “*”
                        # A \ character
      *?                # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
   \b                # Assert position at a word boundary
   bold              # Match the characters “bold” literally
   \b                # Assert position at a word boundary
)

希望这可以帮助。

于 2012-05-29T04:50:13.640 回答
2

请记住,font-weight:bold;它也可以设置为整数 700或通过扩展字符串范围以匹配相当多的简写font符号。

\b(?:font.*?:[^;]*?\b(bold|700))\b

摆弄

于 2012-05-29T05:06:04.160 回答
0

试试这个:

/font-weight\: bold|font-weight\:bold|font-weight \:bold|font-weight \: bold/
于 2012-05-29T04:59:07.537 回答
0

试试这个:

RegularExpressionValidator.ValidationExpression = "font-weight\s?:\s?bold"
于 2012-05-29T05:03:57.233 回答
0

您不能为此使用正则表达式。CSS 可能包含这样的字符串

.foo{ content: "font-weight:bold;" }

你需要一个合适的解析器。幸运的是,浏览器有一个并提供API来访问各个 CSS 规则。

于 2012-05-29T05:32:08.590 回答