JSFiddle - React, Tailwind, and code Playground

by Denis

HTML

<pre></pre>

JavaScript

var str = `<div draggable="true" onclick="return false;"  id="item_col-2" class="layout_el wtf  col-2 gap">
    <div class="g-grid" ondrop="drop(event, this)" ondragover="allowDrop(event)">123</div>
    <div class="g-grid" ondrop="drop(event, this)" ondragover="allowDrop(event)">321</div>
</div>`;

// Допустимые классы
const allowClasses = new Set(['col-2', 'gap', 'g-grid']);

const wrapper = document.createElement('div');
wrapper.innerHTML = str;
wrapper.querySelectorAll('*').forEach(el => {
  [...el.attributes].forEach(attr => {
    if (attr.name !== 'class') {
      el.removeAttribute(attr.name);
    } else {
      [...new Set(el.classList)].filter(x => !allowClasses.has(x)).forEach(cls => {
        el.classList.remove(cls);
      });

    }
  });
});
document.querySelector('pre').textContent = wrapper.innerHTML;