JSFiddle - React, Tailwind, and code Playground

by Greggg

JavaScript

let str = `<p data-uk="test" class="test" data-attribute-uk="any-string">paragraph<a>link</a></p>`;
let div = document.createElement("div");
div.innerHTML=str;

div.querySelectorAll("*").forEach(function(el){
  let nodeNames = []
  for (const attributeKey in el.attributes) {
    nodeNames.push(el.attributes[attributeKey].nodeName)
  }
  for (const name of nodeNames) {
    if (name && !name.match(/^data-uk/gm)) el.removeAttribute(name);
  }
});
let sanitizedString = div.innerHTML
console.log(sanitizedString)