Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用正则表达式匹配css元素_Css_Regex - Fatal编程技术网

如何使用正则表达式匹配css元素

如何使用正则表达式匹配css元素,css,regex,Css,Regex,我在jQuery的head部分调用indexOf。我想看看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\

我在
jQuery
的head部分调用
indexOf
。我想看看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"
    
    试试这个:

    /font-weight\: bold|font-weight\:bold|font-weight \:bold|font-weight \: bold/
    
    RegularExpressionValidator.ValidationExpression = "font-weight\s?:\s?bold"
    

    记住
    font-weight:bold
    也可以设置为或通过一个速记来扩展字符串的范围以匹配相当多的字符串

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

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

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

    您需要一个合适的解析器。幸运的是,浏览器有一个并提供访问单个CSS规则的功能。

    [^;]*?
    太过宽松(请参阅fiddle:),为什么不使用“\s*”?我仍然不明白为什么
    \s*
    在这里不够
    \b字体权重\s*:\s*粗体\b
    应该可以。我还是不明白
    [^;\{}]
    的优点。它仍然会匹配类似于
    字体重量:invalidcssbutitstillsays bold
    的内容@Kobi jinx。因为如果任何其他样式包含在文本中,该模式仍然不会失败。@Cylian,您的意思是要匹配
    font-style:normal;还有:大胆?我担心这里的方法可能会有很大的问题。您试图实现的是什么,这无法通过测试像
    var isBold=$(someSelector).css('font-weight')=='bold'
    )这样的元素来实现?好吧,我提供的代码只是一个示例。实际上,当主体css设置为direction:rtl时,我要做的是了解页面方向是否为rtl。jquery不读取direction属性,只读取dir属性。
    $(someSelector).css('direction')
    似乎适合我。。。?它还可以与继承一起正常工作(例如,样式是在我正在检查的元素的父元素上设置的)