如何在多个正则表达式之间进行匹配并在它们之间进行替换

时间:2015-12-23 16:32:19

标签: javascript regex

在用户提供的任何科学论文中,程序必须.....

  1. 找到:
  2. 1.a - 数字引文,例如

      

    而不是从汇总文本中复制完整句子,而不是这些   方法要么压缩句子 [1,4-6] ,要么重新生成新句子   从头开始的句子 [3]

    1.b - 论文末尾的APA参考文献只是authoe的名字+年。

    1. 将数字转换为APA,例如
    2.   

      而不是从汇总文本中复制完整句子,而不是这些   方法要么压缩句子(Jing 2000; Knight and Marcu   2000; Sporleder和Lapata 2005; Steinberger和Ježek2006),或   从头开始重新生成新句子(McKeown et al 1999)

      我认为正则表达式是:

      "\[(\d.*?)\]"
      

      用于数字引用。

      "\d+([\.]([\ ])(([\D*])*([\,]))*([\ ][\w][\.]))|[\d]{4}"
      

      用于APA引用方式。

      我的问题是如何在第一种模式中替换第二种模式?

1 个答案:

答案 0 :(得分:1)

String.prototype.replace()与回调函数一起使用,该函数将数字字符串(逗号分隔,可能是范围)拆分为数字数组。然后遍历数字并使用您的其他正则表达式查找作者/年。然后加入这些字符串并将其返回以进行替换。

var string = `Instead of reproducing full sentences from the summarized text, these methods either compress the sentences [1, 4-6], or re-generate new sentences from scratch [3].
1. Jing, H.: Sentence Reduction for Automatic Text Summarization. In Proceedings of the 6th Applied Natural Language Processing Conference, Seattle, USA, 2000, pp. 310–315.
3. McKeown et al: Sentence Reduction for Automatic Text Summarization. In Proceedings of the 6th Applied Natural Language Processing Conference, Seattle, USA, 1999, pp. 310–315.
4. Knight and Marcu.: Sentence Reduction for Automatic Text Summarization. In Proceedings of the 6th Applied Natural Language Processing Conference, Seattle, USA, 2000, pp. 310–315.
5. Sporleder and Lapata: Sentence Reduction for Automatic Text Summarization. In Proceedings of the 6th Applied Natural Language Processing Conference, Seattle, USA, 2005, pp. 310–315.
6. Steinberger and Ježek: Sentence Reduction for Automatic Text Summarization. In Proceedings of the 6th Applied Natural Language Processing Conference, Seattle, USA, 2006, pp. 310–315.`;

string.replace(/\[(\d[^\]]*)\]/g, function(matches, reference) {
    var numbers = [],
        authors = [];

    reference.split(/,\s*/g).forEach(function(number) {
        if(number.indexOf('-') !== -1) {
            var range = number.split('-');
            for(var i = range[0]; i <= range[1]; i++) {
                numbers.push(i);
            }
        } else numbers.push(number);
    });

    numbers.forEach(function(number) {
        var regex = new RegExp(number + '\\. ([^:]+):.*?(\\d{4})'),
            matches = regex.exec(string);
        authors.push(matches[1] + ' ' + matches[2]);
    });

    return '(' + authors.join('; ') + ')';
});