admin管理员组

文章数量:1355221

I am trying to parse a text file that contains a bunch of test questions/answers to code a multiple choice test taker.

The questions and answers are all on separate lines so I need to read each file line by line and somehow parse it by just using html/javascript/jquery.

How would I do this? THANKS!

The text file has the extension .dat but is actually a text file. Its just the format these e in and there are too many to change... /?17bggsa47u4ukmx

I am trying to parse a text file that contains a bunch of test questions/answers to code a multiple choice test taker.

The questions and answers are all on separate lines so I need to read each file line by line and somehow parse it by just using html/javascript/jquery.

How would I do this? THANKS!

The text file has the extension .dat but is actually a text file. Its just the format these e in and there are too many to change... http://www.mediafire./?17bggsa47u4ukmx

Share Improve this question edited Mar 28, 2012 at 23:52 asked Mar 28, 2012 at 23:44 user1299463user1299463 1
  • please post a sample of the text file. Why are you not using json or XML? – FlavorScape Commented Mar 28, 2012 at 23:48
Add a ment  | 

2 Answers 2

Reset to default 6

try this

function readQAfile(filename){
    $.ajax(filename,
        {
            success: function(file){
                var lines = file.split('\n');
                var questions = [];

                var length = lines.length;
                for(var i = 0; i < length; i+=2){
                    questions.push({
                        question: lines[i],
                        answer: lines[i+1] || "no answer"
                    })
                }
                window.questions = questions;
            }
        }
    );
}

to use this you'll need to be running the website on a server (a local server is fine).

To get started try using regexp.

The following expression will split your text on every $$[number] occasion. From there you can brute force slice and cut and chop your string further.

Example code:

var regex = /(\$\$\d+)/g;
var str = "adasda$$1adadad$$23adsads\nadad\nadad$$3";

console.log(str.split(regex));

["", "$$1", "adad sad", "$$23", "asdad", "$$3", ""]

本文标签: How do you readparse a text file line by line using htmljavascriptStack Overflow