Home > front end >  Certain number question being missed in regex
Certain number question being missed in regex

Time:05-15

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?

CodePudding user response:

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

CodePudding user response:

As a quick fix, in the function quesCleanUp() try to change the line:

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

With:

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

I suspect you got the downvotes because you posted the code with glared typos. It looks like you didn't even try to debug it first.

And probably the function can be boiled down to just one line:

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