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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:19:44+00:00 2026-06-07T13:19:44+00:00

How might I wrap every word in a paragraph with a span while keeping

  • 0

How might I wrap every word in a paragraph with a span while keeping any nested links functioning? Using the code below from here I get close, but splitting on whitespace inserts a span between the a and the href resulting in something like this:

<p><span><a< span> <span>href="#"&gt;this</span></a<></span></p>

which obviously renders the link unusable.

$('p').each(function() {

var text = $(this).html().split(/\s+/),//split on space
    len = text.length,
    result = []; 

for( var i = 0; i < len; i++ ) {
    result[i] = '<span>' + text[i] + '</span>';
}
$(this).html(result.join(' '));
});

Jsfiddle here which perhaps better illustrates my point. Thanks!

  • 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-07T13:19:45+00:00Added an answer on June 7, 2026 at 1:19 pm

    The problem is you don’t want to randomly be inserting "<span>" tags inside of other tags. A resulting html like: <img <span>src="blahblah"</span>> is going to be an issue, were you ever to get one.

    You can use a regex to match out the tags in the HTML and only add <span> tags to everything else. Probably not perfect, but something like:

    $('p').each(function() {
    
        var tagRE = /([^<]*)(<(?:\"[^\"]*\"|'[^']*'|[^>'\"]*)*>)([^<]*)/g,
            match,
            result = [],
            i = 0;
    
        while(match = tagRE.exec($(this).html())) {
            var text1 = match[1].split(/\s+/),
                len1 = text1.length;
    
            var text2 = match[3].split(/\s+/),
                len2 = text2.length;
    
            for(var tIdx = 0; tIdx < len1; tIdx++ )
                result[i++] = '<span>' + text1[tIdx] + '</span>';           
    
            result[i++] = match[2];
    
            for(var tIdx = 0; tIdx < len2; tIdx++ )
                result[i++] = '<span>' + text2[tIdx] + '</span>';          
        }
    
        $(this).html(result.join(' '));
    
    });
    

    http://jsfiddle.net/5wabK/

    It’s probably a little inefficient but I had to use a group at the beginning and the end in order to capture everything before the first tag and after the last one.

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

Sidebar

Related Questions

I'm using lettering.js to wrap <span> elements around each letter in a string. I'm
Is it possible to write code that wrap any node.js i/o-callback with my own
might be a silly question nonetheless: I'm playing around with the following code: $a='a';
How might a Java program wrap a value into a scala.Either? For example, how
I am using a search library which advises keeping search handle object open for
This might seem like a very easy answer but I cant seem to wrap
How might one implement C# yield return using Scala continuations? I'd like to be
try { ///// here is the code that might throw erros. If I discover
Have spent a week trying to wrap my head around Yii framework, but while
Might be my question sounds more naive. But I wanted to know if it

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.