Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 657593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:50:29+00:00 2026-05-13T22:50:29+00:00

I’ve been beating my head against this reg ex for the longest time now

  • 0

I’ve been beating my head against this reg ex for the longest time now and am hoping someone can help. Basically I have a WYSIWYG field where a user can type formatted text. But of course they will copy and paste form word/web/etc. So I have a JS function catching the input on paste. I got a function that will strip ALL of the formatting on the text which is nice, but I’d like to have it leave tags like p and br so it’s not just a big mess.

Any regex ninjas out there? Here is what I have so far and it works. Just need to allow tags.

o.node.innerHTML=o.node.innerHTML.replace(/(<([^>]+)>)/ig,"");
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:50:29+00:00Added an answer on May 13, 2026 at 10:50 pm

    The browser already has a perfectly good parsed HTML tree in o.node. Serialising the document content to HTML (using innerHTML), trying to hack it about with regex (which cannot parse HTML reliably), then re-parsing the results back into document content by setting innerHTML… is just a bit perverse really.

    Instead, inspect the element and attribute nodes you already have inside o.node, removing the ones you don’t want, eg.:

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

    Defined as:

    // 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;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.