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?
A simple regex could be:
If you’d like to check only inside the style attribute in a tag, try the following PHP test code:
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:
Basically, I’m asking to match the keyword
position:orbackground: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: