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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:39:10+00:00 2026-05-31T01:39:10+00:00

i have the following text: <span term=db6ff2ffe2df7b8cfc0d9542bdce27dc class=yellowback>Lorem</span> <span term=e78f5438b48b39bcbdea61b73679449d class=yellowback>ipsum</span> dolor sit amet,

  • 0

i have the following text:

    <span term="db6ff2ffe2df7b8cfc0d9542bdce27dc" class="yellowback">Lorem</span> <span term="e78f5438b48b39bcbdea61b73679449d" class="yellowback">ipsum</span> dolor sit amet,   consectetur adipiscing elit.
Ut ut mattis sapien.   Suspendisse at felis nisl.   Vestibulum nec risus leo,   in consectetur dolor.   Duis suscipit arcu quis nibh dapibus gravida.   Ut vel rhoncus neque.   Sed et dolor quis est sollicitudin vulputate.   Nam vehicula,   tortor at consectetur laoreet,   nulla erat ultrices dui,   vehicula varius odio sem sed ligula.
Vivamus porttitor odio sed ligula cursus non placerat dolor posuere.
Pellentesque vitae metus vel dolor lobortis feugiat.   Nunc faucibus commodo viverra.   Aliquam porta nisl eu turpis vulputate id laoreet odio lobortis.   Proin sit amet neque nibh,   eget tincidunt est.   Etiam accumsan erat at mauris lacinia porta.
Suspendisse auctor,   quam sit amet congue consequat,   dolor orci placerat diam,   sed ultricies diam ipsum nec tortor.   Vestibulum egestas ipsum ut leo fermentum imperdiet.   Mauris varius iaculis magna,   id luctus risus vestibulum vel.

I would like to split it into words but if you look closely some words may be contained within some tags. What i want to do is this: if the word is within a tag it should treat the tag overall as the word. Right now I have the following regex to accomplish this:

(<span.+>|\w+|<\/span>)

This works but if there are 2 adiacent tags it will capture them both and treat them as a word which is not something i would want.

I am not fond of using Regex for this thing but it seems the most appropriate solution giving the fact that it has to be in javascript and there is no way I can use a 3rd party library. I am open however to a different approach, using some sort of algorithm…if not Regex is just fine.

A satisfactory result would be the following

["<span term=\"db6ff2ffe2df7b8cfc0d9542bdce27dc\" class=\"yellow\">Lorem</span>", "<span term=\"e78f5438b48b39bcbdea61b73679449d\" class=\"yellow\">Ipsum</span>", "is", "simply", "dummy", "text", "of", "the", "printing", "and", "typesetting", "industry", ".
     ", "Lorem", "Ipsum", "has", "been", "the", "industry", " ' ", "s", "standard", "<span term=\"e78f5438b48b39bcbdea61b73679449d\" class=\"yellow\">Ipsum</span>", "text", "ever", "since", "the", "1500s", ",
     ", "when", "an", "unknown", "printer", "took", "a", "galley", "of", "type", "and", "scrambled", "it", "to", "make", "a", "type", "specimen", "book", ".
     ", "It", "has", "survived", "not", "only", "five", "centuries", ",  ", "but", "also", "the", "leap", "into", "electronic", "typesetting", ",
     ", "remaining", "essentially", "unchanged", ".  ", "It", "was", "<span term=\"e78f5438b48b39bcbdea61b73679449d\" class=\"yellow\">Ipsum</span>", "in", "the", "1960s", "with", "the", "release", "of", "Letraset", "sheets", "containing", "Lorem", "Ipsum", "passages", ",  ", "and", "more", "recently", "with", "desktop", "publishing", "software", "like", "Aldus", "PageMaker", "including", "versions", "of", "Lorem", "Ipsum", ".
"]

Not a good result would be the following:

["<span term=\"db6ff2ffe2df7b8cfc0d9542bdce27dc\" class=\"yellow\">Lorem</span> <span term=\"e78f5438b48b39bcbdea61b73679449d\" class=\"yellow\">Ipsum</span>", "is", "simply", "dummy", "text", "of", "the", "printing", "and", "typesetting", "industry", ".
         ", "Lorem", "Ipsum", "has", "been", "the", "industry", " ' ", "s", "standard", "<span term=\"e78f5438b48b39bcbdea61b73679449d\" class=\"yellow\">Ipsum</span>", "text", "ever", "since", "the", "1500s", ",
         ", "when", "an", "unknown", "printer", "took", "a", "galley", "of", "type", "and", "scrambled", "it", "to", "make", "a", "type", "specimen", "book", ".
         ", "It", "has", "survived", "not", "only", "five", "centuries", ",  ", "but", "also", "the", "leap", "into", "electronic", "typesetting", ",
             ", "remaining", "essentially", "unchanged", ".  ", "It", "was", "<span term=\"e78f5438b48b39bcbdea61b73679449d\" class=\"yellow\">Ipsum</span>", "in", "the", "1960s", "with", "the", "release", "of", "Letraset", "sheets", "containing", "Lorem", "Ipsum", "passages", ",  ", "and", "more", "recently", "with", "desktop", "publishing", "software", "like", "Aldus", "PageMaker", "including", "versions", "of", "Lorem", "Ipsum", ".
    "]

Notice how the 2 spans form 1 array element in the 2nd example while in the first one they are 2 different elements.

  • 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-31T01:39:11+00:00Added an answer on May 31, 2026 at 1:39 am

    How about:

    str.split(/(<span[^>]*>[^<]+<\/span>|\w+)/)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following HTML <p><input id=revenue type=text value=100000 /><span id=howmuch><a href= class=ka_button small_button
Currently I have the following element <h6 class=header> Chance of Precipitation: <span data-bind=text: PrecipitationLabel></span>
I have the following script: $(.Text).contents().each(function () { $(this).replaceWith($(this).text() .replace(/\[([^\]]*)\]/g, '<span class=IT_Symbol style=display:inline;border: 1px
I have the following text on my page: <span class=UTCDate>Date.UTC(2011, 8, 7, 7, 9,
I have the following HTML <div class=outerBox> <div class=innerBox> //text </div> <div class=clickMe> <span
I have the following markup: <div id=selectable1> <span class=drag>Some Text</span> <span class=drag>Some Text</span> <span
Suppose I have the following HTML element: <span id='kuku' class='lala bubu' value='xyz'>some text</span> I
I have the following markup: <li id=CN2787> <img class=fav_star src=images/fav.png> <span>Text, text and more
I have the following: $(span.findme).each(function() { $(<li>).text($(this).text()).appendTo(ul); }); How can I, using the above,
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>

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.