我正在调用indexOf
中的头部jQuery
。我想看看 CSS 是否包含任何font-weight:bold
属性。问题是,该属性可以有 4 个更改:
font-weight:bold
font-weight: bold
font-weight :bold
font-weight : bold
如何使用正则表达式匹配这个?
试试这个
\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
)
希望这可以帮助。
试试这个:
/font-weight\: bold|font-weight\:bold|font-weight \:bold|font-weight \: bold/
试试这个:
RegularExpressionValidator.ValidationExpression = "font-weight\s?:\s?bold"
您不能为此使用正则表达式。CSS 可能包含这样的字符串
.foo{ content: "font-weight:bold;" }
你需要一个合适的解析器。幸运的是,浏览器有一个并提供API来访问各个 CSS 规则。