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 7165547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:16:06+00:00 2026-05-28T14:16:06+00:00

I have the following bit of HTML <div class=article>this is a div article content</div>

  • 0

I have the following bit of HTML

<div class="article">this is a div article content</div>

which is being “tagged” by an HTML-agnostic program on the words div, class and article, resulting in:

<<hl>div</hl> <hl>class</hl>="<hl>article</hl>">this is a <hl>div</hl> <hl>article</hl> content</<hl>div</hl>>

although what I really need is:

<div class="article">this is a <hl>div</hl> <hl>article</hl> content</div>

Since the output is utter garbage (even tools like HTML Tidy choke on it), I figured a regex replace would help strip out the extra <hl>s inside the HTML tag:

replace(/<([^>]*)<hl>([^<]*?)<\/hl>([^>]*?)>/g, '<$1$2$3>')

Now, this works but only replaces the first occurrence in the tag, that is, the div:

<div <hl>class</hl>="<hl>article</hl>">this is a <hl>div</hl> <hl>article</hl> content</div>

My question is: how do I replace all <hl>s inside the tag, so as to make sure the HTML remains valid?

Additional notes:

  • I don’t need the tag attributes at all (i.e. class="article" can disappear)
  • I can change <hl> and </hl> for any other strings
  • Yes, the output comes from Solr

UPDATE: I accepted jcollado’s answer, but I needed this in Javascript. This is the equivalent code:

var stripIllegalTags = function(html) {

  var output = '',
    dropChar,
    parsingTag = false;

  for (var i=0; i < html.length; i++) {
    var character = html[i];

    if (character == '<') {
      if (parsingTag) {
        do {
          dropChar = html[i+1];
          i++;
        } while (dropChar != '>');
        continue;
      }
      parsingTag = true;
    } else if (character == '>') {
      parsingTag = false;
    }

    output += character;

  }

  return output;

}
  • 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-28T14:16:07+00:00Added an answer on May 28, 2026 at 2:16 pm

    Maybe the piece of code below is helpful for you:

    class HTMLCleaner(object):
        def parse(self, html):
            output = []
            parsing_tag = False
    
            html = iter(html)
            for char in html:
                if char == '<':
                    if parsing_tag:
                        drop_char = html.next()
                        while drop_char != '>':
                            drop_char = html.next()
                        continue
                    parsing_tag = True
                elif char == '>':
                    parsing_tag = False
    
                output.append(char)
    
            return ''.join(output)
    
    html = '<<hl>div</hl> <hl>class</hl>="<hl>article</hl>">this is a <hl>div</hl> <hl>article</hl> content</<hl>div</hl>>'
    
    parser = HTMLCleaner()
    print parser.parse(html)
    

    The output for the given input is:

    <div class="article">this is a <hl>div</hl> <hl>article</hl> content</div>
    

    which I believe is what you’re looking for.

    The code basically drops all tags when another tag hasn’t been parsed yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code generating content on my site: <div class=content id=links> <?php
I have the following html <td valign=top id=oddnumberedcolumns> <div><a><span class=myspan>line 1 without linebreak</span></a></div> <div><a><span
I have the following HTML: <div class=item> <img src=http://mintywhite.com/images/wg/0904/05rssfeedicons/rss-feed-icons11.jpg/> TEXT NODE <span class=some_class>What the
I have the following bit of code, simply: $(function() { $('a.add-photos-link').live('click', function(e) { $(this).colorbox({
I have problems with the following bit of javascript/jquery code: this.droppable = function(){ $('.imageWindow
I have the following situation in code, which I suspect may be a bit
I have the following HTML scenario: <div> <input id=txt0 type=text /><input type=checkbox id=chk0 /></div>
I have the following form in cakephp: <div class=quickcontactDisplay> <?php echo $form->create('Enquiry',array('action'=>'add','class'=>'quickcontact')); echo $form->hidden('visitor_id',
I have an HTML Document that looks a bit like this, only is far
I have the following HTML: <table class=products> <tr> <td>Product description probably goes here</td> <td><a

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.