Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

87
Views
Delete a specific and generated random attribute from HTML string

Would it be possible to change a string containing this line of HTML:

'<div _ngcontent-isd-c78="" pdroppable="formElements" class="panel" ng-reflect-scope="formElements" id="divPanelId" style="color: red">DivContent</div>'

To this:

'<div id="divPanelId" style="color: red">DivContent</div>'

I want to remove _ngcontent-??, pdroppable, class and ng-reflect-scope.

Is it possible using JavaScript with Regex?

Note: _ngcontent-isd-c78 is changed randomly. For example, _ngcontent-sfw-c53, _ngcontent-isw-c21. So _ngcontent-???? where ???? is a random value

almost 4 years ago · Santiago Trujillo
3 answers
Answer question

0

If you have DOM access use it.

NOTE: If you mess with Angular attributes you can break things

const div = document.getElementById("divPanelId");
const keep = ["style","id"]; 
div.getAttributeNames().forEach(attr => {
  if (keep.includes(attr)) return;
  console.log("removing",attr)
  div.removeAttribute(attr)
})  
console.log(div.outerHTML)
<div _ngcontent-isd-c78="" pdroppable="formElements" class="panel" ng-reflect-scope="formElements" id="divPanelId" style="color: red">DivContent</div>

almost 4 years ago · Santiago Trujillo Report

0

you can use regex

var text = `<div _ngcontent-isd-c78="" _ngcontent-isd-c80=""  _ngcontent-whd-c50="asdad" pdroppable="formElements" class="panel" ng-reflect-scope="formElements" id="divPanelId" style="color: red">DivContent</div>`;
        
            const one = /(.?)ngcontent(.*?)="(.*?)"/g;
            const two = /pdroppable(.*?)="(.*?)"/g;
            const three = /class="(.*?)"/g;
            const four = /ng-reflect-scope(.*?)="(.*?)"/g;
            text = text.replace(one, "").replace(two, "").replace(three, "").replace(four, "");
            console.log(text)

almost 4 years ago · Santiago Trujillo Report

0

Here is a way to bring the html string into the DOM world, remove the unwanted attributes, and then convert it back to a string.

const inputDivStr = '<div _ngcontent-isd-c78="" pdroppable="formElements" class="panel" ng-reflect-scope="formElements" id="divPanelId" style="color: red">DivContent</div>';

const parent = document.createElement('div');
parent.innerHTML = inputDivStr;
const divElem = parent.firstChild;
for (let attrName of divElem.getAttributeNames()) {
  if (shouldRemoveAttr(attrName)) {
    divElem.removeAttribute(attrName);
  }
}

const outputDivStr = parent.innerHTML;
console.log(outputDivStr);

function shouldRemoveAttr(attrName) {
  const regex = /_ngcontent-|pdroppable|class|ng-reflect-scope/;
  return attrName.match(regex);
}

almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!