Reputation:
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
Upvotes: 2
Views: 12539
Reputation: 546
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).
Upvotes: 6
Reputation: 11740
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", ""]
Upvotes: 0