<([a-z][a-z0-9]*)[^>]*?(/?)>
token | explanation |
---|---|
< | match < at beginning of tags |
( | start capture group $1 - tag name |
[a-z] | match a through z |
[a-z0-9]* | match a through z or 0 through 9 zero or more times |
) | end capture group |
[^>]*? | match anything other than > , zero or more times, not-greedy (wont eat the / ) |
(/?) | capture group $2 - / if it is there |
> | match > |
Add some quoting, and use the replacement text <$1$2>
it should strip any
text after the tagname until the end of tag />
or just >
.
Before
HTML containing style
attributes.
<p style="padding:0px;">
<strong style="padding:0;margin:0;">hello</strong>
</p>
After
HTML attributes removed.
<p>
<strong>hello</strong>
</p>
PHP Example
$with_attr = '<p style="padding:0px;"><strong style="padding:0;margin:0;">hello</strong></p>';
$without_attr = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(/?)>/i",'<$1$2>', $with_attr);
echo $without_attr
<p><strong>hello</strong></p>
stackoverflow post.
Nice regex. For js I had to escape the forward slash to make it a literal character
<([a-z][a-z0-9]*)[^>]*?(\/?)>