3

I'm calling indexOf on the head section in jQuery. I want to see if CSS contains any font-weight:bold property. The issue is, that property can have 4 alters:

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

How can I match this using regex?

3
  • 1
    I'm concerned that the approach here may be very problematic. What are you trying to accomplish, that couldn't be accomplished by testing an element like var isBold = $(someSelector).css('font-weight') == 'bold'? Commented May 29, 2012 at 5:18
  • well, the code i provided were an example only. actually, what im trying to do is to understand if page direction is rtl or not when body css set to direction:rtl. jquery doesn't read direction property, only dir property. Commented May 29, 2012 at 6:20
  • $(someSelector).css('direction') seems to work for me...? It also works correctly with inheritance (e.g. the style is set on a parent of the element I'm inspecting) Commented May 29, 2012 at 19:45

5 Answers 5

4

Try this

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

or would be better to use:

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

Explanation

\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
)

Hope this helps.

Sign up to request clarification or add additional context in comments.

7 Comments

[^;]*? is too permissive (see fiddle: jsfiddle.net/HcpSQ/1), why not use `\s* instead?
I still don't understand why \s* isn't enough here... \bfont-weight\s*:\s*bold\b should do.
I still don't understand the advantage of [^;\{\}]. It would still match something like font-weight: invalidcssbutitstillsays-bold. @Kobi jinx.
Because if any other styles come within text, still that pattern would not fail.
@Cylian, you mean you want to match font-style: normal; something-else: bold;?
|
2

Remember that font-weight:bold; could also be set as an integer 700 or through a shorthand font notation which expands the range of strings to match quite a bit.

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

Fiddled

Comments

0

Try this:

/font-weight\: bold|font-weight\:bold|font-weight \:bold|font-weight \: bold/

Comments

0

Try this:

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

Comments

0

You cannot use regular expressions for that. CSS may contain strings like so

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

You need a proper parser. Luckily the browser has one and offers APIs to access the individual CSS rules.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.