字符串返回True / False

时间:2018-11-11 21:17:24

标签: javascript html string

所以我试图用给定的指令模仿我的代码,但是我不确定一个人如何返回这些指定的字符串示例,我已经尽了最大的努力,但是我对如何下一步,如何处理?

我添加了对与错的语句,但是我不确定如何使JS发挥应有的作用或从何处开始。

Instructions

我的HTML:

<!<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Strings True/False</title>
<script src="prototype.js"></script>
<script src="strings.js"></script>
<style>
  body {
  margin-left: 15%;
  font-family: monospace;
  }
  .r { color: blue};
</style>
</head>
<body>
    <h1> String Test </h1>
    <p> Example 1 </p>
    <p> <input id="input1" type="text"> </p>
    <p id="regex1"> Regular Expression : [**Replace with regex**] </p>
    <p id="result1" class="r"> Result: </p>
    <p> <button id="b1"> Press to evaluate your string </button> </p>
    <hr>
    <p> Example 2 </p>
    <p> <input id="input2" type="text"> </p>
    <p id="regex2"> Regular Expression : [**Replace with regex**] </p>
    <p id="result2" class="r"> Result: </p>
    <p> <button id="b2"> Press to evaluate your string </button> </p>
    <hr>
    <p> Example 3 </p>
    <p> <input id="input3" type="text"> </p>
    <p id="regex3"> Regular Expression : [**Replace with regex**]</p>
    <p id="result3" class="r"> Result: </p>
    <p> <button id="b3"> Press to evaluate your string </button> </p>
    <hr>
    <p> Example 4 </p>
    <p> <input id="input4" type="text"> </p>
    <p id="regex4"> Regular Expression :[**Replace with regex**] </p>
    <p id="result4" class="r"> Result: </p>
    <p> <button id="b4"> Press to evaluate your string </button> </p>
    <hr>
    <p> Example 5 </p>
    <p> <input id="input5" type="text"> </p>
    <p id="regex5"> Regular Expression : [**Replace with regex**] </p>
    <p id="result5" class="r"> Result: </p>
    <p> <button id="b5"> Press to evaluate your string </button> </p>
</body>
</html>

JS:

    window.onload = function() {
      $("b1").observe("click", myFunction1);
      $("b2").observe("click", myFunction2);
      $("b3").observe("click", myFunction3);
      $("b4").observe("click", myFunction4);
      $("b5").observe("click", myFunction5);
    }

    function myFunction1() {
      // Function re for a number that begins from 0-9 and any number after
      var re = /^[0-9]+/;
      var testString = $("input1").value;

      if (re.test(testString) == true)
      {
        $("result1").innerHTML = "Result: TRUE";
      }
      else {
        $("result1").innerHTML = "Result: FALSE";
      }
    }


    function myFunction2() {

      var re;
      var testString = $("input2").value;

      if (re.test(testString) == true)
      {
        $("result2").innerHTML = "Result: TRUE";
      }
      else {
        $("result2").innerHTML = "Result: FALSE";
      }
    }


    function myFunction3() {

      var re;
      var testString = $("input3").value;

      if (re.test(testString) == true)
      {
        $("result3").innerHTML = "Result: TRUE";
      }
      else {
        $("result3").innerHTML = "Result: FALSE";
      }
    }

    function myFunction4() {

      var re;
      var testString = $("input4").value;

      if (re.test(testString) == true)
      {
        $("result4").innerHTML = "Result: TRUE";
      }
      else {
        $("result4").innerHTML = "Result: FALSE";
      }
    }

    function myFunction5() {

      var re;
      var testString = $("input5").value;

      if (re.test(testString) == true)
      {
        $("result5").innerHTML = "Result: TRUE";
      }
      else {
        $("result5").innerHTML = "Result: FALSE";
      }
    }

1 个答案:

答案 0 :(得分:0)

我的理解是,您正在构建一个练习页面来测试用户对正则表达式的了解。在这种情况下,当您这样做时:

  var re;
  var testString = $("input1").value;

  if (re.test(testString) == true)

,实际上,您所做的与尝试实现的相反。 re应该是RegExp本身,而testString是要针对所述RegExp测试的字符串。您需要做的是

1)使用RegExp constructor从用户输入中的字符串创建RegExp对象,例如var re = new RegExp($("input1").value)

2)拥有一组预制的测试字符串(屏幕截图中的“示例”),针对它们输入的RegExp进行测试

3)在html中输出结果