正则表达式中遗漏了某些数字问题

Certain number question being missed in regex

我有以下 if 语句,它删除了数字的第一个实例,后跟句点。但是,我注意到它没有捕捉到其中的一些(例如“16.”、“23.”、“24.”等)并且不确定为什么。

函数如下:

function quesCleanUp(ques){
  //Checks the first instance of "." and removes it and the number
  if(ques.match(/[0-9]\./g)?.length > 1){//(ques.match(/./g)?.length > 1){
    var quesClean = ques.replace(/^[^\.]*\./, '').trim();
  } else{
    var quesClean = ques.trim();
  }

  return quesClean;
}

下面的 for 循环从 google 形式中提取问题:

for (var i = 0; i < items.length; i++) {
var item = items[i];
switch(item.getType()) {
  case FormApp.ItemType.MULTIPLE_CHOICE:
    var question = item.asMultipleChoiceItem();
    var ques = quesCleanUp(question.getTitle().trim());//replace(/\s/g, "");
    var question_type = "Multiple Choice";
    var optns = [];
    var answr;
    var answers = question.getChoices();
    answer_val = false;
     for (var j = 0; j < answers.length; j++) {
      var clean = answers[j].getValue().trim();
      optns.push(clean);
      if(answers[j].isCorrectAnswer()){
        answr = answers[j].getValue().trim();
        for(var x = 0; x < optns.length; x++){
            if(answr == optns[x]){
              answer_val = true;
              break;
            }
        }
      }
    }
    var multiJSON = makeJSON(ques, question_type, optns, answr);
    console.log("JSON1: " + JSON.stringify(multiJSON));
    constructedJSON[i+1] = multiJSON;
    break;
  case FormApp.ItemType.CHECKBOX:
    var question = item.asCheckboxItem();
    //var ques = question.getTitle().trim();//.replace(/\s/g, "");
    var ques = quesCleanUp(question.getTitle().trim());//replace(/\s/g, "");
    var question_type = "CheckBox";
    var optns = [];
    var answr = [];
    var answers = question.getChoices();
    
     for (var j = 0; j < answers.length; j++) {
      var clean = answers[j].getValue().trim();//replace(/\s/g, "");
      optns.push(clean);
      if(answers[j].isCorrectAnswer()){
        answr.push(answers[j].getValue().trim());
      }
    }
    var checkJSON = makeJSON(ques, question_type, optns, answr);
    console.log("JSON2: " + JSON.stringify(checkJSON));
    constructedJSON[i+1] = checkJSON;
    break;
  case FormApp.ItemType.PARAGRAPH_TEXT:
    var question = item.asParagraphTextItem();
    //var ques = question.getTitle().trim();//.replace(/\s/g, "");
    var ques = quesCleanUp(question.getTitle().trim());//replace(/\s/g, "");
    var question_type = "free response";
    var optns = [];
    var answr;
    var paraJSON = makeJSON(ques, question_type, optns, answr);
    console.log("JSON3: " + JSON.stringify(paraJSON));
    constructedJSON[i+1] = paraJSON;
    break;
  case FormApp.ItemType.TEXT:
    var question = item.asTextItem();
    //var ques = question.getTitle().trim();
    var question_type = "free response";
    var ques = quesCleanUp(question.getTitle().trim());//replace(/\s/g, "");
    var optns = "";
    var answr = "";
    var textJSON = makeJSON(ques, question_type, optns, answr);
    console.log("JSON4: " + JSON.stringify(textJSON));
    constructedJSON[i+1] = textJSON;
    break;
}

下例为题型16.人生的意义是什么?

以及预期输出:生命的意义是什么?

尝试使用/[0-9]+./g 来捕捉一个以上的数字

作为快速修复,在函数 quesCleanUp() 中尝试更改行:

if(ques.match(/[0-9]\./g)?.length > 1){//(ques.match(/./g)?.length > 1){

有:

if (ques.match(/^[0-9]+\./g).length > 0) {

我怀疑你得到了反对票,因为你发布的代码有明显的拼写错误。看起来您甚至没有先尝试调试它。锦上添花的是你接受了一个错误的答案。

也许这个函数可以归结为一行:

const quesCleanUp = q => q.replace(/^\d+\./,'').trim();

这是它的工作原理:

var questions = ['1. aaa', '16. What', '23. That', 'No nums'];
const quesCleanUp = q => q.replace(/^\d+\./,'').trim();

questions.forEach(q => console.log(quesCleanUp(q)));

预期输出:

aaa
What
That
No nums