JavaScript测验的模式匹配

Pattern Matching for JavaScript Quiz

本文关键字:模式匹配 JavaScript      更新时间:2023-09-26

我有一个文本框答案测试,我想将"模式匹配"应用于此,例如,它是不区分大小写的,接受轻微拼写错误等。

我可以在下面定义的答案中应用"I"和[a-z]吗?

function init() 
          {
    questions = new Array()
            questions[1] = "What records need to be set up in order to place TV bookings on the system?"
            questions[2] = "What do you need first to spot-match?"
    userAnswers = new Array 
    userAnswers[1] = "c[a-z]t, p[a-z]t, t[a-z]s, c[a-z]n" 
    userAnswers[2] = "t[a-z]e r[a-z]d"

            reference = new Array()
            reference[1] = "C______, P_______, T______s, C_______?"
            reference[2] = "T________ R_________"

          } 

如有任何线索和帮助,我们将不胜感激。。。感谢

您可以根据需要尝试以下格式,希望这对您有所帮助。

    public static final String EXAMPLE_TEST = "caaac";
    public static final String EXAMPLE_TEST1 = "taaae";
    public static void main(String[] args) {
    System.out.println(EXAMPLE_TEST.matches("[c][a-z]{4,}"));
    System.out.println(EXAMPLE_TEST1.matches("[t][a-z]{3}[e]"));
    }

你可以参考这个链接,因为你没有提到单词的大小,所以我就这样给出了。

您可以这样尝试。

  function A(input)
  {
 // var code = [c][a-z]{4,};
  var code = [t][a-z]{3}[e];
  if(input.value.match(code))
  {
  alert(Correct..);
  return true;
  }
  else
  {
  alert('Please give correctly');
  return false;
  }

}