0

Very new to the world of regex and looking for a solution that will strip out all style tags from the HTML string. Example:

const myString = '<div>The quick brown <span style="color:brown;">fox</span> jumps over the lazy <span style="font-weight:bold">dog</span>.</div>';

console.log(myString.replaceAll(/style=".*?\"/, ''));

The desired output would be to strip out everything starting with style=" and the closing "

//<div>The quick brown <span>fox</span> jumps over the lazy <span>dog</span></div>.

Severely struggling with regex, any help appreciated!

dangre00
  • 440
  • 3
  • 8
  • 1
    `console.log(myString.replaceAll(/ style=".*?\"\B/g, ''));` where `\B` «Matches, without consuming any characters, at the position between two characters matched by \w or \W.» – GrafiCode Jun 03 '22 at 21:35
  • 1
    `g` is needed, otherwise it will raise this error: `String.prototype.replaceAll called with a non-global RegExp argument` – GrafiCode Jun 03 '22 at 21:36
  • 1
    GrafiCode - brilliant! Thanks so much!! – dangre00 Jun 03 '22 at 21:39
  • 1
    https://stackoverflow.com/questions/4541573/what-are-non-word-boundary-in-regex-b-compared-to-word-boundary – GrafiCode Jun 03 '22 at 21:42

0 Answers0