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 8445593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:41:41+00:00 2026-06-10T09:41:41+00:00

I am trying to delete the some elements from the html style= attribute. <div

  • 0

I am trying to delete the some elements from the html style=”” attribute.

<div style="color: red; background: #000; position: fixed; top:0; left: 0;">Hey</div>

I want to cut this HTML so it will delete
position element, and background element.

Example:

<div style="color: red; top:0; left: 0;">Hey</div>

How can I do that on Regex?

  • 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-10T09:41:43+00:00Added an answer on June 10, 2026 at 9:41 am

    A simple regex could be:

    /background:.*?;|position:.*?;/
    

    If you’d like to check only inside the style attribute in a tag, try the following PHP test code:

    <?php
    $str = '<div style="color: red; background: #000; position: fixed; top:0; left: 0;">Hey</div>';
    echo preg_replace('/(<.*?style=.*?)background:.*?;|position:.*?;(.*?")/','$1$2',$str);
    ?>
    

    You also asked to match the case when the attribute doesn’t have a semicolon (unique attribute, last attribute). To match the case without semicolon, we should assume that is either the only attribute or is the last one. In both cases we’ll have the following regexes working:

    position:[^;]*?("|')
    background:[^;]*?("|')
    

    Basically, I’m asking to match the keyword position: or background: followed by any char except the semicolon, repeated zero or more times until a quote (single or double) is found.

    This covers the case we called “without semicolon”.

    The following code should be working for all cases, it’s not optimized and is only for clarity and example. It consist of a chain of calls:

    $str = preg_replace('/(<.*?style.*?)(position:[^;]*?)("|\')/','$1$3',$str);
    $str = preg_replace('/(<.*?style.*?)(background:[^;]*?)("|\')/','$1$3',$str);
    $str = preg_replace('/(<.*?style.*?)(position:.*?;)(.*?")/','$1$3',$str);
    $str = preg_replace('/(<.*?style.*?)(background:.*?;)(.*?")/','$1$3',$str);
    echo $str;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying some way to optimize following sql statement: exe_sql DELETE FROM tblEvent_type
I'm trying to recursively delete some versions/files from a specific branch. find delivers the
I am trying to remove elements from a std::list and keep some stats of
I am trying to write some code that uses SQL to delete rows from
I'm trying to set background colours on a few different elements in an html
i am trying to delete some files using a batch file.. (winxp) my problem
I'm trying to delete some files with unicode characters in them with batch script
I am trying to delete MySQL table rows from a Web page using Perl.
I'm trying to delete a specific line by id from a file in C++
I'm trying to delete certain lines from a file. My file is a .txt

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.