Skip to content
Advertisement

Certain number question being missed in regex

I have the following if statement that removes the first instances of a number followed by the period. However, I am noticing it is missing to catch some of them (ex. “16.”, “23.”, “24.”, etc.) and not sure why.

Here is the function:

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

The following for loop extracts the question from the google form:

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

The following example is the type of question 16. What is the meaning of life?

And the expected output: What is the meaning of life?

Advertisement

Answer

Try using /[0-9]+./g to catch more than one digit

Advertisement