Rashid
Rashid

Reputation: 1740

html in string remove inside of style - regex

NOTE: This is a string and not an actual DOM element

I have this string

<p><span contenteditable="true"><span contenteditable="false" data-role="img-crop"><img alt="" data-role="img-resizable" data-x="0" data-y="0" draggable="false" height="146" src="temporary/e78f4ed1-9f8b-443b-b6b0-678c6dad97bd.jpg"  width="293" /></span></span> &lt;span&gt;small&lt;/span&gt;<span contenteditable="true"><span contenteditable="false" data-role="img-crop"><img alt="" data-role="img-resizable" data-x="0" data-y="0" draggable="false" height="208" src="temporary/eeaf1f44-5c17-48f6-9204-210b64f79f98.jpg" style="cursor: pointer" width="554" /></span></span> </p><p>dsadsadas<br /></p>

and I want to remove the content of style to be like this style="" but i dont know how using regex. Note only regex and not using removeAttr() of jquery

I already tried this regex /style="[^"]*/g but it removes the style string which is what i dont want to since i am comparing it to the original content that has an empty style

Please help

Upvotes: 0

Views: 40

Answers (1)

Jack Bashford
Jack Bashford

Reputation: 44107

Then just use replace with a capturing group:

str = str.replace(/(style=")(?:[^"]*)(")/g, "$1$2"); 

Upvotes: 1

Related Questions