简体   繁体   English

字符串返回真/假

[英]String Returns True/False

So I'm trying to mimic my code with the instructions given but I'm not sure as to how one returns these specified string examples, I've done as much as I could but I'm stuck as to what to do next, how does one approach it?所以我试图用给出的说明来模仿我的代码,但我不确定如何返回这些指定的字符串示例,我已经做了尽可能多的工作,但我不知道下一步该怎么做,如何接近它?

I've added true and false statements but I'm not sure as to how to get JS to act the way it needs to be or where to begin it.我已经添加了正确和错误的陈述,但我不确定如何让 JS 以它需要的方式行事或从哪里开始。

指示

My HTML:我的 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: 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";
      }
    }

My understanding is that you're building an exercise page to test user's knowledge of regular expressions.我的理解是您正在构建一个练习页面来测试用户对正则表达式的了解。 In that case, when you're doing this:在这种情况下,当你这样做时:

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

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

, you're actually doing the opposite of what you're trying to achieve. ,您实际上正在做与您想要实现的相反的事情。 re is supposed to be the RegExp itself and testString is a string you're testing against the said RegExp. re应该是 RegExp 本身,而testString是您针对上述 RegExp 测试的字符串。 What you need to do is你需要做的是

1) create RegExp object from a string in a user input, with the use of RegExp constructor , ie something like var re = new RegExp($("input1").value) 1) 从用户输入的字符串创建 RegExp 对象,使用RegExp 构造函数,即类似var re = new RegExp($("input1").value)

2) having a set of your pre-made test strings ('examples' in your screenshot), test them against the RegExp they typed in 2) 拥有一组预先制作的测试字符串(屏幕截图中的“示例”),根据他们输入的 RegExp 对它们进行测试

3) output the result in the html 3)在html中输出结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM