I have a list of all Shakespeare's sonnets, and I perform the search function of each sonnet. However, I want to be able to search for them using Arabic numbers (for example, "/ sonnet 122". TXT is formatted as follows:
I This is a sonnet II This is a second sonnet
I am using node right now to try to do this, but I tried from yesterday to no avail. My last attempts yesterday used 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 = /[AZ]{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:
data.replace(found, function(s, i){ return numArr[i]; });
Then I tried to use a for loop. I did not save this 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 follows:
replace(abc, 123) -> 1bc, a2c, ab3
How to make it repeat every event in the data and save it? Then saving it to the new txt should be easy.
(Also, my RegExp finds only plural character numbers to avoid replacing the single I that can be found at the end of the line.)