首页 文章

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

提问于
浏览
2

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

  • 找到:

1.a - 数字引用,例如

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

1.b - 论文末尾的APA参考文献以及authoe的名称年份 .

  • 将数字转换为APA,例如

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

我认为正则表达式是:

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

用于数字引用 .

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

适用于APA引用风格 .

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

1 回答

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

相关问题