I have a list of all Shakespeare sonnets and I'm making a function to search for each sonnet. However, I want to be able to search them using arabic numbers (for example "/sonnet 122". The .txt is formatted this way:
I
This is a sonnet
II
This is a second sonnet
I am using node right now to try to do it, but I've been trying since yesterday to no avail. My last attempts yesterday were using the 'replace' method as such:
'use strict';
//require module roman-numerals, which converts roman to arabic
var toArabic = require('roman-numerals').toArabic;
//require file-handling module
var fs = require('fs');
fs.readFile('sonn.txt', 'utf8', function (err,data) {
if (err) {
console.log(err);
} else {
var RN = /[A-Z]{2,}/g;
var found = data.match(RN); //finds all roman numbers and puts them in an array
var numArr = [];
for (var i = 0; i < found.length; i++ ){
numArr.push(toArabic(found[i])); //puts all arabic numbers in numArr
}
for (var e = 0; e < found.length; e++){
data.replace(found, found.forEach((x, i)=> {
toArabic(x)
}
});
Then I tried replacing them with:
data.replace(found, function(s, i){
return numArr[i];
});
Then I tried with a for loop. I didn't keep that code, but it was something like:
for(var i=0;i<found.length;i++){
data.replace(found, numArr[i]);
}
The last code replaces each number and then erases the data and replaces the next number as such:
replace(abc, 123) -> 1bc, a2c, ab3
How do I make it iterate each occurrence in the data and keep it? Then saving it to a new txt should be easy.
(Also, my RegExp only finds multiple character roman numbers to avoid replacing lonely I's that could be found at the end of a line.)