How to remove all html tags in javascript with exceptions?

I’ve been beating my head against this reg-ex for a long time, and I hope that someone can help. Basically, I have a WYSIWYG field where the user can print formatted text. But of course, they will copy and paste the word / web / etc form. This way, I have a JS function that captures the input when pasting. I have a function that will remove ALL text formatting that would be nice, but I would like it to leave tags like p and br, so this is not just a big mess.

Are there any regular ninja expressions? Here is what I still have, and it works. Just need to enable tags.

o.node.innerHTML=o.node.innerHTML.replace(/(<([^>]+)>)/ig,"");
+3
source share
2

HTML o.node. HTML ( innerHTML), ( HTML), innerHTML... .

, o.node, , , .:

filterNodes(o.node, {p: [], br: [], a: ['href']});

:

// Remove elements and attributes that do not meet a whitelist lookup of lowercase element
// name to list of lowercase attribute names.
//
function filterNodes(element, allow) {
    // Recurse into child elements
    //
    Array.fromList(element.childNodes).forEach(function(child) {
        if (child.nodeType===1) {
            filterNodes(child, allow);

            var tag= child.tagName.toLowerCase();
            if (tag in allow) {

                // Remove unwanted attributes
                //
                Array.fromList(child.attributes).forEach(function(attr) {
                    if (allow[tag].indexOf(attr.name.toLowerCase())===-1)
                       child.removeAttributeNode(attr);
                });

            } else {

                // Replace unwanted elements with their contents
                //
                while (child.firstChild)
                    element.insertBefore(child.firstChild, child);
                element.removeChild(child);
            }
        }
    });
}

// ECMAScript Fifth Edition (and JavaScript 1.6) array methods used by `filterNodes`.
// Because not all browsers have these natively yet, bodge in support if missing.
//
if (!('indexOf' in Array.prototype)) {
    Array.prototype.indexOf= function(find, ix /*opt*/) {
        for (var i= ix || 0, n= this.length; i<n; i++)
            if (i in this && this[i]===find)
                return i;
        return -1;
    };
}
if (!('forEach' in Array.prototype)) {
    Array.prototype.forEach= function(action, that /*opt*/) {
        for (var i= 0, n= this.length; i<n; i++)
            if (i in this)
                action.call(that, this[i], i, this);
    };
}

// Utility function used by filterNodes. This is really just `Array.prototype.slice()`
// except that the ECMAScript standard doesn't guarantee we're allowed to call that on
// a host object like a DOM NodeList, boo.
//
Array.fromList= function(list) {
    var array= new Array(list.length);
    for (var i= 0, n= list.length; i<n; i++)
        array[i]= list[i];
    return array;
};
+9

-, , . HTML ( > >), . , , /.

-, .

-, lookahead :

o.node.innerHTML=o.node.innerHTML.replace(/<(?!\s*\/?(br|p)\b)[^>]+>/ig,"");

:

<

(?!\s*\/?(br|p)\b) , , /, br p, . , lookahead , <pre> <param ...>.

[^>]+ ,

> .

, , - .

, ( )

<pre> <a href="dot.com"> </a> </pre>

<p> < p > < /br > <br /> <br> ..

.

+3

Source: https://habr.com/ru/post/1735725/


All Articles