如何替换阿拉伯语等价字符串中的所有罗马数字?

我有一个莎士比亚十四行诗的清单,我正在制作一个搜索每个十四行诗的函数。 但是,我希望能够使用阿拉伯数字(例如“/ sonnet 122”)搜索它们.txt的格式如下:

I

This is a sonnet

II

This is a second sonnet

我现在正在使用节点来尝试这样做,但自昨天以来我一直在尝试无效。 我昨天的最后一次尝试是使用'替换'方法:

'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)
    }
});

然后我尝试将它们替换为:

data.replace(found, function(s, i){
    return numArr[i];
});

然后我尝试了一个for循环。 我没有保留这些代码,但它是这样的:

for(var i=0;i<found.length;i++){
    data.replace(found, numArr[i]);
}

最后的代码替换每个数字,然后擦除数据并替换下一个数字,如下所示:

replace(abc, 123) -> 1bc, a2c, ab3

我如何让它迭代数据中的每一次出现并保留它? 然后将它保存到新的txt应该很容易。

(另外,我的RegExp仅查找多个字符的罗马数字,以避免在行尾找到替代孤独的I的情况。)


你必须把被替换的字符串写回来,并且你可以使用一个回调函数replace()

'use strict';

var toArabic = require('roman-numerals').toArabic;
var fs = require('fs');

fs.readFile('sonn.txt', 'utf8', function (err,data) {
    if (err) {
        console.log(err);
    } else {
        data = data.replace(/[A-Z]{2,}/g, function(x) {
            return toArabic(x);
        });
    }
});

这里有一些更符合罗马文的正则表达式


如果使用String.prototype.replace ,则可以使用正则表达式和自定义替换函数。 你只需要返回值作为替换,这是toArabictoArabic

var data = 'InnThis is a sonnetnnIInnThis is a second sonnet';

//========================

var toArabic = (function () {
  var forEach = Array.prototype.forEach;


  /**
   * Converts a roman number to its arabic equivalent.
   *
   * Will throw TypeError on non-string inputs.
   *
   * @param {String} roman
   * @return {Number}
   */
  function toArabic (roman) {
    if (('string' !== typeof roman) && (!(roman instanceof String))) throw new TypeError('toArabic expects a string');

    // Zero is/was a special case. I'll go with Dionysius Exiguus on this one as
    // seen on http://en.wikipedia.org/wiki/Roman_numerals#Zero
    if (/^nulla$/i.test(roman) || !roman.length) return 0;

    // Ultra magical regexp to validate roman numbers!
    roman = roman.toUpperCase().match(/^(M{0,3})(CM|DC{0,3}|CD|C{0,3})(XC|LX{0,3}|XL|X{0,3})(IX|VI{0,3}|IV|I{0,3})$/);
    if (!roman) throw new Error('toArabic expects a valid roman number');
    var arabic = 0;

    // Crunching the thousands...
    arabic += roman[1].length * 1000;

    // Crunching the hundreds...
    if (roman[2] === 'CM') arabic += 900;
    else if (roman[2] === 'CD') arabic += 400;
    else arabic += roman[2].length * 100 + (roman[2][0] === 'D' ? 400 : 0);


    // Crunching the tenths
    if (roman[3] === 'XC') arabic += 90;
    else if (roman[3] === 'XL') arabic += 40;
    else arabic += roman[3].length * 10 + (roman[3][0] === 'L' ? 40 : 0);

    // Crunching the...you see where I'm going, right?
    if (roman[4] === 'IX') arabic += 9;
    else if (roman[4] === 'IV') arabic += 4;
    else arabic += roman[4].length * 1 + (roman[4][0] === 'V' ? 4 : 0);
    return arabic;
  };
  return toArabic;
})();

//====================

var RN = /[A-Z]{1,2}(?=n)/g;
var newData = data.replace(RN, toArabic);
document.body.innerText = newData;

链接地址: http://www.djcxy.com/p/37233.html

上一篇: How to replace all roman numbers in a string for the arabic equivalent?

下一篇: Using ColdFusion to sign data for single sign