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

  • Home
  • SEARCH
  • 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 1898850
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:50:35+00:00 2026-05-17T06:50:35+00:00

does anyone know of a good regular expression to remove events from html. For

  • 0

does anyone know of a good regular expression to remove events from html.

For example the string:
"<h1 onmouseover="top.location='http://www.google.com">Large Text</h1>
Becomes
"<h1>Large Text</h1>
So HTML tags are preserved but events like onmouseover, onmouseout, onclick, etc. are removed.

Thanks in Advance!

  • 1 1 Answer
  • 0 Views
  • 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-17T06:50:44+00:00Added an answer on May 17, 2026 at 6:50 am

    How about:

    data.replace(/ on\w+="[^"]*"/g, '');
    

    Edit from the comments:

    This is intended to be run on your markup as a one time thing. If you’re trying to remove events dynamically during the execution of the page, that’s a slightly different story. A javascript library like jQuery makes it extremely easy, though:

    $('*').unbind();
    

    Edit:

    Restricting this to only within tags is a lot harder. I’m not confident it can be done with a single regex expression. However, this should get you by if no one can come up with one:

    var matched;
    
    do
    {
        matched = false;
        data = data.replace(/(<[^>]+)( on\w+="[^"]*")+/g,
            function(match, goodPart)
            { 
                matched = true;
                return goodPart;
            });
    } while(matched);
    

    Edit:

    I surrender at writing a single regex for this. There must be some way to check the context of a match without actually capturing the beginning of the tag in your match, but my RegEx-fu is not strong enough. This is the most elegant solution I’m going to come up with:

    data = data.replace(/<[^>]+/g, function(match)
    {
        return match.replace(/ on\w+="[^"]*"/g, '');
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know a good online resource for example of R code? The programs
Does anyone know of any good tutorials on regular expressions using boost? I have
Does anyone know a good Java lib that will hook into SVN so I
Does anyone know any good tool that I can use to perform stress tests
Does anyone know a good resource to concisely explain the different types of lists
Does anyone know any good tutorial about using NAnt for native code build process
Does anyone know any good and free .NET Windows Controls Library
Does anyone know a good C++ class to read (and possibly also write) INI
Does anyone know a good tool that can be run on a page (for
Does anyone know a good solution for implementing a function similar to Ruby's timeout

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.