0

I have an html text and I have to delete all styles on it, for example:

let myString= "<div style='color: red;'>Hello world</div><p>Some text here</p><div style='border: 0px;'>hello world 2!</div>";

In this text I want to delete all occurrences that start with style=' and finish on ', so I'm removinig all styles from my text, so any word could between style= and ' how would be the regex for this.

let formatedString = myString.replace(regexHere, '');

The result should be:

<div>Hello world</div><p>Some text here</p><div>hello world 2!</div>

Why is not duplicate: The suggested post is about tags, mine is about "params" inside a tag, so for a regex's newbie (like me) is different.

IBot
  • 356
  • 3
  • 20
  • Possible duplicate of [RegEx match open tags except XHTML self-contained tags](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Hamms Mar 15 '18 at 21:27
  • please read: https://stackoverflow.com/a/1732454/220949 – arod May 20 '18 at 16:05

1 Answers1

2

You can use style='[^']*'

  • style=' matches style=' literally

  • [^']* matches anything but ' 0 or more times

  • ' matches ' literally

Demo:

let myString= "<div style='color: red;'>Hello world</div><p>Some text here</p><div style='border: 0px;'>hello world 2!</div>";
let formatedString = myString.replace(/ style='[^']*'/g, '');
console.log(formatedString);
Zenoo
  • 12,670
  • 4
  • 45
  • 69