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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:57:47+00:00 2026-05-30T06:57:47+00:00

Using Javascript, I want to take the content of a string and wrap every

  • 0

Using Javascript, I want to take the content of a string and wrap every single word with a set of <span> tags. Each span tag will have a unique ID attribute that I’ll generate. If a word is already encased in a span tag with an id, leave it alone. If it has a span tag without an id, I need to put an id in the span.

So, the following:

this is <b>a</b> <span>bunch</span> of <span id="aopksd"><i>text</i></span>

Becomes:

<span id="dwdkwkd">this</span> <span id="zdkdokw">is</span> <span id="rrrsass"><b>a</b></span> <span id="lwokdw">bunch</span> <span id="poeokf">of</span> <span id="aopksd"><i>text</i></span>

Ideas on the best way to accomplish this? I’m doing it server-side in node.js. I prefer a non-jquery method.

EDIT:
The id is something I will generate on the server. I just used placeholder fake ids for now. I will use my own globally unique id.

  • 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-30T06:57:48+00:00Added an answer on May 30, 2026 at 6:57 am

    You can try this, but you might have to inspect the parameter a if you want to make a special treatment for your word already in html tag :

    var str = "Bonjour, je m'appelle Francis et je suis le plus bel homme du monde​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​";
    var regex = /\S+/g;
    
    var id = 0;
    
    var result = str.replace(regex, function(a) {
        return "<span id=" + (++id) + ">" + a + "</span>";
    });​​​​​​​​​​
    
    alert(result);
    

    live demo : http://jsfiddle.net/NtNtj/

    EDIT: If you don’t want to overwrite existing ID, you can try this

    var str = "Bonjour, je m'appelle <b>Francis</b> et <span id=\"existing\">je</span> suis le plus bel homme du monde";
    var regex = /(<.+?<\/.+?>|\S+)/g;
    
    var id = 0;
    
    var result = str.replace(regex, function(a) {
    
        var m = (/<(\w+)([^>]*)>([^<]*)<\/\w+>/).exec(a);
    
        if (m !== null && m[1] === "span" && m[2].test(/id=/)) 
            return a;
    
        return "<span id=" + (++id) + ">" + a + "</span>";
    });
    
    console.log(result);
    

    http://jsfiddle.net/NtNtj/8/

    EDIT: If you can have multiple word in a tag like and you still want to wrap each word in between, you can call recursively the replace function with the value inside the tag as so :

    var str = "Bonjour, <b>je m'appelle Francis</b> et <span id=\"existing\">je</span> suis le plus bel homme du monde";
    var regex = /(<.+?<\/.+?>|\S+)/g;
    
    var id = 0;
    
    var result = str.replace(regex, function(a) {
    
        var m = (/<(\w+)([^>]*)>([^<]*)<\/\w+>/).exec(a);
    
        if (m !== null && m[1] === "span" && m[2].test(/id=/)) 
            return a;
    
        if (m !== null)
            return "<" + m[1] + m[2] + ">" + m[3].replace(regex, arguments.callee) + "</" + m[1] + ">";
    
        return "<span id=" + (++id) + ">" + a + "</span>";
    });
    
    console.log(result);
    

    live demo: http://jsfiddle.net/francisfortier/NtNtj/9/

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

Sidebar

Related Questions

Let's say using Javascript, I want to match a string that ends with [abcde]*
I want to add some style to head tag in html page using javascript.
I'm using javascript and would like to take a URL string that I have
Suppose a HTML document has a iframe. Using javascript,I want to detect ,on load
I want to handle F1-F12 keys using JavaScript and jQuery. I am not sure
I want to call a google service using javascript with a keyword and a
I want to print a web page using JavaScript. But I do not want
i want to enable facebox jquery using javascript. here in normal html <a href=stairs.jpg
I want to be able to close an alert box automatically using Javascript after
I want to do some client side validation using javascript in ASP.NET page. I

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.