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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:22:22+00:00 2026-05-23T00:22:22+00:00

For example I have a html string: <span class=no>1172</span><span class=r>case</span> primary_key_prefix_type How to wrap

  • 0

For example I have a html string:

<span class="no">1172</span><span class="r">case</span> primary_key_prefix_type

How to wrap every element which doesn’t have tag by Nokogiri like this:

<span class="no">1172</span><span class="r">case</span> <span>primary_key_prefix_type</span>
  • 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-23T00:22:22+00:00Added an answer on May 23, 2026 at 12:22 am

    This doesn’t feel like the most elegant solution, but it works:

    require 'nokogiri'
    
    # Given a node, find each whitespace-delimited word
    # and wrap it in the supplied markup
    def wrap_text( node, wrapper='<span />' )
      wrapper = Nokogiri::XML::DocumentFragment.parse(wrapper).children.first
      node.xpath('child::text()').each do |text_node|
        text_node.swap( text_node.text.gsub(/(\s*)(\S+)(\s*)/) do
          "#{$1}#{
            wrapper.clone.tap{ |w| w.inner_html = $2 }.to_html
          }#{$3}"
        end )
      end
      node
    end    
    
    # Testing
    html = Nokogiri::HTML '<body>
      <p><span class="no">1172</span><span class="r">case</span> primary_key_prefix_type</p>
      <p>Hello <b>cool</b> world #42!</p>
    </body>'
    
    html.search('p').each{ |para| wrap_text(para) }
    puts html.at('body')
    #=> <body>
    #=>   <p><span class="no">1172</span><span class="r">case</span> <span>primary_key_prefix_type</span></p>
    #=>   <p><span>Hello</span> <b>cool</b> <span>world</span> <span>#42!</span></p>
    #=> </body>
    

    Edit: More examples:

    # If your lines don't have element wrapping them...
    raw = [
      '<span class="no">1172</span><span class="r">case</span> primary_key',
      'Hello <b>cool</b> world #42!'
    ]
    puts raw.map{ |line| wrap_text(Nokogiri::HTML(line).at('body')).inner_html }
    #=> <span class="no">1172</span><span class="r">case</span> <span>primary_key</span>
    #=> <p>Hello <b>cool</b> world #42!</p>
    
    # If your lines each have exactly one element wrapping them...
    wrapped = [
      '<a><span class="no">1172</span><span class="r">case</span> primary_key</a>',
      '<b>Hello <b>cool</b> world #42!</b>'
    ]
    body = Nokogiri::HTML(wrapped.join("\n")).at('body')
    puts body.children.map{ |e| wrap_text(e) }
    #=> <a><span class="no">1172</span><span class="r">case</span> <span>primary_key</span></a>
    #=> <b><span>Hello</span> <b>cool</b> <span>world</span> <span>#42!</span></b>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of legacy documents that are HTML-like. As in, they look
I have a class that takes a bit of time to start up (makes
I have an extremely simple example trying to use Data Annotations with unobtrusive javascript
I have a website on Domain A that has a page with an html
I have read http://developer.android.com/guide/practices/screens_support.html and http://developer.android.com/reference/android/util/DisplayMetrics.html and I somehow left with a gap here.
If I have in my model class a property of type DateTime how can
I am using Microsoft's XAML/HTML converter to convert HTML from a database into a
How can I get all the unclosed tags in a given string, prefferably in
We have a public calendar for our company set up in an Exchange 2007
I'm using ASP.NET with C# 2.0. I have created some objects for a database

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.