0

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

4
  • It would be easier to use DOMParser for this. Commented Sep 23, 2022 at 8:11
  • 2
    Don't use a regex to parse HTML. Also, the _ng... attributes are generally inserted by Angular, and are required for the framework to function. You can't remove them. Commented Sep 23, 2022 at 8:13
  • @RoryMcCrossan i already know that. I need html string of page with clean code. Commented Sep 23, 2022 at 8:25
  • @gog this is not html. Think of it as a string Commented Sep 23, 2022 at 8:28

3 Answers 3

3

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>

Sign up to request clarification or add additional context in comments.

Comments

2

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)

2 Comments

_ngcontent-isd-c78 : this is not specific. it could be '_ngcontent-whd-c13', '_ngcontent-thw-c74' ...etc
okey i edited. check again
1

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.