How do you read/parse a text file line by line usi

2019-07-20 04:13发布

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 come in and there are too many to change... http://www.mediafire.com/?17bggsa47u4ukmx

2条回答
地球回转人心会变
2楼-- · 2019-07-20 05:01

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", ""]

查看更多
Melony?
3楼-- · 2019-07-20 05:05

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).

查看更多
登录 后发表回答