-2
const lessonText = "<div><blockquote>&quot;&quot;</blockquote><p>Heni &quot;</p><blockquote>no quotation</blockquote><span>hi</span><blockquote><span style=\"font-family:Courier New,Courier,monospace;\">row.names(vol) &lt;- c(&quot;</span></blockquote></div>"

Regex to capture and replace &quot; with " when &quot is inside blockquote tag, that means i don't want &quot here, <p>Heni &quot;</p>, to be changed to ".

enter image description here

Henok Tesfaye
  • 8,287
  • 13
  • 47
  • 84

2 Answers2

0

You'd need a proper HTML parser to support nested tags, for example, blockquote tags could be nested.

Here is a solution if you can live with the limitation of not supporting corner cases. It uses nested replaces, the outer one to identify blockquote tags with its content, the inner one to take action on the blockquote content:

const lessonText = "<div><blockquote>&quot;&quot;</blockquote><p>Heni &quot;</p><blockquote>no quotation</blockquote><span>hi</span><blockquote><span style=\"font-family:Courier New,Courier,monospace;\">row.names(vol) &lt;- c(&quot;</span></blockquote></div>"
let result = lessonText.replace(/(<blockquote>)(.*?)(<\/blockquote>)/g, function(m, g1, g2, g3) {
  return g1 + g2.replace(/&quot;/g, '"') + g3;
});
console.log('Result: ' + result);

Result: <div><blockquote>""</blockquote><p>Heni &quot;</p><blockquote>no quotation</blockquote><span>hi</span><blockquote><span style="font-family:Courier New,Courier,monospace;">row.names(vol) &lt;- c("</span></blockquote></div>

Explanation of outer replace regex:

  • (<blockquote>) -- capture group 1: opening blockquote tag
  • (.*?) -- capture group 2: non-greedy scan over content until:
  • (<\/blockquote>) -- capture group 3: closing blockquote tag
  • /g flag -- replace all patterns

Note:

  • Limitation: This fails with nested tags
  • If you expect attributes for the blockquote you could use regex (<blockquote( [^>]*)?>) instead (which does not support corner cases like <blockquote tile="<gotcha>!">)
  • If you expect newlines in the blockquote content you can use regex ([\s\S]*?) instead.

Explanation of inner replace regex:

  • &quot; -- capture literal &quot; text (you could use string "&quote" instead)
  • /g flag -- replace all patterns
Peter Thoeny
  • 7,379
  • 1
  • 10
  • 20
-2

You can try :

lines = lines.replace(<blockquote.*?<\/blockquote/g, (fullMatch, g1) => {
    return fullMatch.replace(/&quot;/, '"');
});
user1075296
  • 581
  • 3
  • 10