I need to replace the value of "p-type" to "p-kind" only in the "button" tags using regex, for example:
input:
<button p-type="foo">
anything
</button>
output:
<button p-kind="foo">
anything
</button>
the "p-type" property may not be the first one, but even then it should be changed to "p-kind", for example:
input :
<button anythingProperty p-type="foo">
anything
</button>
output:
<button anythingProperty p-kind="foo">
anything
</button>
If the tag is not a button the "p-type" remains, for example a div with this property will not be changed.
I can change using the following expression: (p-type)([a-zA-Z0-9:]*).
But that changes for all and I would like to group only the ones that are <button></button>
p-typeis an invalid HTML5 attribute, right? Usedata-*attributes instead. Also, use a proper DOMParser, not RegExp.(<button\b[^<>]* p-)type(="[^"]*"[^<>]*>)regex101.com/r/r8lmwD/1 and replace with$1kind$2but consider using a dom parser.