Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8528581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:50:00+00:00 2026-06-11T08:50:00+00:00

I am still not able to use regular expressions by heart, thus could not

  • 0

I am still not able to use regular expressions by heart, thus could not find a final solution to strip out all styles from <p style=””>…</p> using RegEx with Javascript, but leave color and background-color if they exist.

What I found:

1. Remove complete style=”…” element with RegEx:

htmlString = (htmlString).replace(/(<[^>]+) style=".*?"/i, '');

2. Remove certain styles with RegEx:

htmlString = (htmlString).replace(/font-family\:[^;]+;?|font-size\:[^;]+;?|line-height\:[^;]+;?/g, '');

Challenge: In case, we remove all styles assigned (no color exists), and style is empty (we have style=”” or style=” “), the style attribute should be removed as well.

I guess we need two lines of code?

Any help appreciated!


Example 1 (whitelisted “color” survives):

<p style="font-family:Garamond;font-size:8px;line-height:14px;color:#FF0000;">example</p>

should become:

<p style="color:#FF0000;">example</p>


Example 2 (all styles die):

<p style="font-family:Garamond;font-size:8px;line-height:14px;">example</p>

should become:

<p>example</p>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:50:01+00:00Added an answer on June 11, 2026 at 8:50 am

    First, the proof of concept. Check out the Rubular demo.

    The regex goes like this:

    /(<[^>]+\s+)(?:style\s*=\s*"(?!(?:|[^"]*[;\s])color\s*:[^";]*)(?!(?:|[^"]*[;\s])background-color\s*:[^";]*)[^"]*"|(style\s*=\s*")(?=(?:|[^"]*[;\s])(color\s*:[^";]*))?(?=(?:|[^"]*)(;))?(?=(?:|[^"]*[;\s])(background-color\s*:[^";]*))?[^"]*("))/i
    

    Broken down, it means:

    (<[^>]+\s+)                           Capture start tag to style attr ($1).
    
    (?:                                   CASE 1:
    
        style\s*=\s*"                     Match style attribute.
    
        (?!                               Negative lookahead assertion, meaning:
            (?:|[^"]*[;\s])               If color found, go to CASE 2.
            color\s*:[^";]*
        )
    
        (?!
            (?:|[^"]*[;\s])               Negative lookahead assertion, meaning:
            background-color\s*:[^";]*    If background-color found, go to CASE 2.
        )
    
        [^"]*"                            Match the rest of the attribute.
    
    |                                     CASE 2:
    
        (style\s*=\s*")                   Capture style attribute ($2).
    
        (?=                               Positive lookahead.
            (?:|[^"]*[;\s])
            (color\s*:[^";]*)             Capture color style ($3),
        )?                                if it exists.
    
        (?=                               Positive lookahead.
            (?:|[^"]*)                    
            (;)                           Capture semicolon ($4),
        )?                                if it exists.
    
        (?=                               Positive lookahead.
            (?:|[^"]*[;\s])
            (background-color\s*:[^";]*)  Capture background-color style ($5),
        )?                                if it exists.
    
        [^"]*(")                          Match the rest of the attribute,
                                          capturing the end-quote ($6).
    )
    

    Now, the replacement,

    \1\2\3\4\5\6
    

    should always construct what you expect to have left!

    The trick here, in case it’s not clear, is to put the “negative” case first, so that only if the negative case fails, the captures (such as the style attribute itself) would be populated, by, of course, the alternate case. Otherwise, the captures default to nothing, so not even the style attribute will show up.

    To do this in JavaScript, do this:

    htmlString = htmlString.replace(
    
        /(<[^>]+\s+)(?:style\s*=\s*"(?!(?:|[^"]*[;\s])color\s*:[^";]*)(?!(?:|[^"]*[;\s])background-color\s*:[^";]*)[^"]*"|(style\s*=\s*")(?=(?:|[^"]*[;\s])(color\s*:[^";]*))?(?=(?:|[^"]*)(;))?(?=(?:|[^"]*[;\s])(background-color\s*:[^";]*))?[^"]*("))/gi,
    
        function (match, $1, $2, $3, $4, $5, $6, offset, string) {
            return $1 + ($2 ? $2       : '') + ($3 ? $3 + ';' : '')
                      + ($5 ? $5 + ';' : '') + ($2 ? $6       : '');
        }
    
    );
    

    Note that I’m doing this for fun, not because this is how this problem should be solved. Also, I’m aware that the semicolon-capture is hacky, but it’s one way of doing it. And one can infer how to extend the whitelist of styles, looking at the breakdown above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read the DateFormatting guide and I'm still not able to get a working
I'm still not sure I totally get how this particular case should work out.
Thanks for replying!! But I am still not able to do it. Error that
I still not understanding the parent role in the android styles I have put
I'm still not that confident when it comes to write unit tests for more
I'm still not practiced in oop.. now I know the importantness of it :)
I know it is still not quite popular, since the spec was released just
I am still not so familiar with interfaces in Java. I know that interface
I'm still not sure this is the correct way to go about this, maybe
I'm still not getting exactly how a custom CursorAdapter should work, so after hard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.