0

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

5
  • 1
    A DOM element never has an empty style attribute, if it is made empty, it is removed. Commented Jul 3, 2019 at 5:55
  • @Teemu I never said it is a DOM element, its a string which has HTML content. adding a NOTE for you to understand Commented Jul 3, 2019 at 5:56
  • "comparing it to the original content" The original content is not in the DOM? Commented Jul 3, 2019 at 5:58
  • @Teemu it is not a DOM. Commented Jul 3, 2019 at 5:59
  • That's awful, make a temporal element and use a HTML parser to manipulate a HTMLString. Commented Jul 3, 2019 at 6:00

1 Answer 1

1

Then just use replace with a capturing group:

str = str.replace(/(style=")(?:[^"]*)(")/g, "$1$2"); 
Sign up to request clarification or add additional context in comments.

1 Comment

nice. this is what I am looking

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.