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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:10:01+00:00 2026-06-15T20:10:01+00:00

I need to check an HTML fragment and replace the ids or classes of

  • 0

I need to check an HTML fragment and replace the ids or classes of HTML elements with other values. Using a regexp doesn’t fit my needs because there can be multiple occurrences of the searched-for class in the text, which I should keep.

For example, I have this HTML:

<div id='sweet'>
  Bla bla sweet bla bla...
</div>

When I replace id='sweet' with ‘bitter’ I want to receive:

<div id='bitter'>
  Bla bla sweet bla bla...
</div>

I can do it with Nokogiri without any problems, but sometimes I get invalid HTML and need to return the markup as it was. The problem is, Nokogiri fixes markup and broken nodes.

Example:

</table>
<div id='sweet'>
  Bla bla sweet bla bla...
</div>

I will receive only this:

<div id='bitter'>
  Bla bla sweet bla bla...
</div>

Example 2:

</div>
<div id='sweet'>
  Bla bla sweet bla bla...
</div>
<table>
  <tr>
    <td>
      Some text

I will get this:

<div id='bitter'>
  Bla bla sweet bla bla...
</div>
<table>
  <tr>
    <td>
      Some text
    </td>
  </tr>
</table>

How can I get this from the second example?:

</div>
<div id='bitter'>
  Bla bla sweet bla bla...
</div>
<table>
  <tr>
    <td>
      Some text
  • 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-06-15T20:10:02+00:00Added an answer on June 15, 2026 at 8:10 pm

    You can use regexes, but with a little more context:

    str = "</div>
    <div id='sweet'>
      Bla bla sweet bla bla...
    </div>
    <table>
      <tr>
        <td>
          Some text"
    
    str.sub /id='sweet'/, "id='bitter'"
    

    will only change the first instance of ‘sweet’.

    Similarly,

    str = "</div>
    <div class='cls1 sweet cls3'>
      Bla bla sweet bla bla...
    </div>
    <table>
      <tr>
        <td>
          Some text"
    
    puts str.sub( /class=['"]([^'"]*)sweet([^'"]*)['"]/ ) { "class='#{$1}bitter#{$2}'" }
    

    handles ‘sweet’ only within a class attribute.

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

Sidebar

Related Questions

I need to check if user submitted HTML contains any JavaScript. I'm using PHP
How do I check if a column exists in a HTML table using JQuery.
I am displaying a shopping cart. I need to check for empty values in
Given that when using an html radio box, to send values to a php
I need to check if the variable texts is defined or not in index.html
I need to grab a string like /html/body/a i need to check the last
I am using jquery UI tab for my project. Kindly check the html code
I need to check if a HTML document is correct, if all tags and
I need to check if an element contains a certain child class using JQUERY.
I'm using HTTP Basic Authentication with Rails 3.0.9 and I need to check if

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.