3

I am new here, please be kind. I am trying to import a text file into a HTML nested list. The HTML output I am trying to achieve would look like this:

    <ul>
        <li>FAVORITE SITES
            <ul>
                <li>eBay</li>
                <li>Google</li>
            </ul>
        </li>
        <li>AVOIDED SITES
            <ul>
                <li>Yahoo</li>
                <li>CraigsList</li>
            </ul>
        </li>
        <li>OTHER SITES
            <ul>
                <li>Alexa</li>
                <li>Amazon</li>
                <li>Jet</li>
            </ul>
        </li>
    </ul>

The text file I am loading using <input type="file"> is not formatted in a usual way - it is not JSON or CSV, it is formatted like below:

FAVORITE SITES {
!Variable=eBay
!Variable=Google
}

AVOIDED SITES {
!Variable=Yahoo
!Variable=CraigsList
}

OTHER SITES {
!Variable=Alexa
!Variable=Amazon
!Variable=Jet
}

Is there any way to load parse and input the values of the text file and load them into the list even though the file is in this non-standard format or will I need to change the format of the text file? Any help will be appreciated, thank you.

2
  • Define 2 sound regexes and parse the content by repeatedly calling its exec() method will be a practical solution. Commented Aug 16, 2017 at 5:57
  • Forgive me, I am a novice but thank you so much for the comment. Is there a regex to be had because the !Variable= is always the same but the headings, example "FAVORITE SITES" are always different. There may be many of them and they will not be known before it could be "FAVORITE SITES { }" or "CHICKEN DINNER { }", etc Commented Aug 16, 2017 at 6:06

2 Answers 2

2

let s = `FAVORITE SITES {
!Variable=eBay
!Variable=Google
}

AVOIDED SITES {
!Variable=Yahoo
!Variable=CraigsList
}

OTHER SITES {
!Variable=Alexa
!Variable=Amazon
!Variable=Jet
}`;

var ul = document.createElement('ul');

ul.innerHTML = s.replace(/(\})/gim, "</ul>\n</li>")
         .replace(/\{/gim, "\n<ul>")
         .replace(/!Variable=(\w+)\s*\n/gim, "<li>$1</li>\n")
         .split(/\s*\n\s*\n/)
         .map(v => v = '\n<li>' + v)
         .join('');

document.body.appendChild(ul);

Sign up to request clarification or add additional context in comments.

Comments

1

Please reference this code snippet;

// assign the file content to s
let s = `FAVORITE SITES {
!Variable=eBay
!Variable=Google
}

AVOIDED SITES {
!Variable=Yahoo
!Variable=CraigsList
}

OTHER SITES {
!Variable=Alexa
!Variable=Amazon
!Variable=Jet
}`;

let a = s.replace(/[\n\r]/g, '').split(/(.+?) \{(.+?)\}/g);

for (let i = 0; i < a.length - 1; i += 3) {
    // a[i + 1] is the outer li text
    let a2 = a[i + 2].split(/!Variable=([^!]+)/g);
    // every 2nd item of a2 is the inner li text
    // or you can filter out '' items
    // you can apply the same trick to a as well, but need to modify for-loop accordingly
    let a3 = a2.filter(v => v !== '');
    // now all items are a3 can be used directly as inner li text
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.