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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:28:03+00:00 2026-05-15T12:28:03+00:00

I have a long string with Headers and Sub-headers . Every header is supposed

  • 0

I have a long string with Headers and Sub-headers. Every header is supposed to have a sub header, but the string pre-processing, does not. I need to manipulate it in such a way that every header has a Sub-header.

Every header that does not have a string has a reference point under different header. The header missing the subheader needs to grab the subheader from the reference’s immediate parent/proceeding header.

Here’s what it looks like:

Header 1
subheader - somedata A
text
reference-header-3
stuff in the way

Header 2
subheader - somedata B
stuff in the way
stuff in the way

Header 3
stuff in the way
stuff in the way
reference-header-5

Header 4
subheader - somedata C
some text

Header 5
more text

I need to get it to be like this:

Header 1
subheader - somedata A
text
reference-header-3
stuff in the way

Header 2
subheader - somedata B
stuff in the way
stuff in the way

Header 3
subheader - somedata A [this is copied from header 1]
stuff in the way
stuff in the way
reference-header-5

Header 4
subheader - somedata C
some text

Header 5
subheader - somedata A [this is copied from header 3]
more text

If anyone know of any string libraries that can help do this that would be awesome. I don’t know how to go about this, I’m thinking of converting them to DOM elements so I can traverse them with jQuery, and then convert back. But that sounds a little icky.

Anyone know how to do this?

Thanks in advance.

  • 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-15T12:28:03+00:00Added an answer on May 15, 2026 at 12:28 pm

    If you have control over the backend, you should have this data sent over as JSON. That would be the best bet. It is also relatively painless. You also shouldn’t be using jQuery for this (its use is rather irrelevant here) unless you were using AJAX to fetch JSON like I described.

    Or you can do something like this:

    var str = "Header 1\none\ntwo\nthree\n\nHeader 2\nsubheader - one\ntwo\nthree\n\nHeader 3\none\ntwo\nthree\n";
    var lines = str.split(/\n/);
    
    var headerMap = {};
    var currentHeader = "";
    
    for(var i = 0; i < lines.length; i++) {
    
       var line = lines[i];
       line = line.replace(/^\s+/, "").replace(/\s+$/, ""); //trim whitespace
    
       if(/^Header [0-9]+$/.test(line) && line != currentHeader) {
          headerMap[line] = new Array();
          currentHeader = line;
       }
    
       //don't add blank lines
       if(line != "") {
          headerMap[currentHeader][headerMap[currentHeader].length] = line;
       }
    }
    

    Now you have a map that is keyed by Header 1 and Header 2 and so on. The value for each key is an array that contains the various subheaders. You can easily iterate over these values and check the very first value in the array to see if it has the subheader - prefix. If not, you can add it.

    I just noticed the second part. I guess what you can do after you do the above is make a second pass and parse out the reference parts and insert the appropriate values there. Slak’s solution might be a one-pass one I think (from the quick read I gave it). OR you can add an else-if to the above code (within the loop where it checks for the hedaer) to check and see if it matches your reference directive. If it does, grab the existing reference and add it what you have (this only works for backward references). If you have forward references then you will need to have a second pass.

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

Sidebar

Ask A Question

Stats

  • Questions 524k
  • Answers 524k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Probably is APC related. For the people having this problem,… May 16, 2026 at 9:52 pm
  • Editorial Team
    Editorial Team added an answer I ran into that issue as well. I spent a… May 16, 2026 at 9:52 pm
  • Editorial Team
    Editorial Team added an answer It seems that Eclipse 3.5.2 doesn't have the feature I… May 16, 2026 at 9:52 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have the above-mentioned error in s1=some very long string............ Does anyone know what
i have a long string dat can contain html tags applying htmlencode will encode
I have a long string (8000 characters) that should contain only hexadecimal and newline
I have been writing a php script that saves a long string within cookie
I want to format an unsigned int to an 8 digit long string with
I have an Android application that generates some HTML which is rendered locally, in
Hey fellas, I have the following VBA code. It gets the values from an
I have a problem with IE7 regarding an ajax call that is made by
long time ago I wrote webservice that is still in use. Now I plan
I have implemented a simple file upload-download mechanism. When a user clicks a file

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.