1

I am having an input string and i need to replace all the tags in the string with blank space and it works fine.but i does't want to select the tag that is inside the "title" attribute.

This is my Input string

var temp='<div title="Minimum Standards for <mark>Child</mark> Protection in Humanitarian Action (CPMS) | content of" >
  <mark>Child</mark>
</div>';

This is my regular expression code to replace the string.

temp = String(temp).replace(/(<mark[^>]*>|<\/mark>)/g, '');

I only need to match the mark tags that is outside the "title" attribute or i just want to ignore the mark tags inside the "title" attribute,I need a regular expression for the same.Thanks in advance.

Pranav s
  • 164
  • 1
  • 11
  • 2
    [*RegEx match open tags except XHTML self-contained tags*](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – RobG Dec 05 '19 at 03:52

1 Answers1

2

One option is to parse the HTML markup as a document instead, and then select all mark tags with querySelectorAll and remove them:

const str = `<div title="Minimum Standards for <mark>Child</mark> Protection in Humanitarian Action (CPMS) | content of" >
  <mark>Child</mark>
</div>`;
const doc = new DOMParser().parseFromString(str, 'text/html');
doc.body.querySelectorAll('mark').forEach(mark => mark.remove());
console.log(doc.body.innerHTML);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • 1
    I think the result of parse + process + innerHTML is likely a practical solution, but may have different (not necessarily "wrong") results compared to a text editing approach. – RobG Dec 05 '19 at 04:02