打印正则表达式Javascript的结果

时间:2018-02-19 09:05:07

标签: javascript regex

我希望根据某些条件显示一些文本(即正则表达式匹配if)并继续提示用户输入一些文本或在提示符下停止。

我使用了do-while,但每次都不打印结果,直到我在提示窗口中输入停止。

document.getelementbyId中给出的所有陈述都没有打印出来。

  • 创建4个正则表达式:

    • 匹配包含A-Z,a-z,0-9和_
    • 中至少一个字符的字符串
    • 匹配不包含0到9之间的数字的字符串
    • 搜索大写字母,后跟一个或多个小写字母,后跟一个空格。
    • 找到一个#符号,然后是5个重复的数字,一个句号和另一个数字
  • 提示用户输入文字。

  • 针对每个正则表达式测试文本。

  • 显示"这是一场比赛"或"不匹配"每次测试后。

  • 重复直到用户输入"停止"并测试最后一次并退出。

这是我的作业,但我试过它不能正常工作。请帮忙。



function matchRegularExpr() {
  var text1 = "";
  document.getElementById("text1").innerHTML = ("<h1>The text is: " + text1 + "</h1></br>");
  document.getElementById("pattern1").innerHTML = ("Match a string that contains at least one character in A-Z, a-z, 0-9 and _</br>");
  document.getElementById("pattern2").innerHTML = "Match a string that does not contain a number between 0 and 9";
  document.getElementById("pattern3").innerHTML = "Search for an uppercase letter, folowed by one or more lowercase letters, followed by a space.";
  document.getElementById("pattern4").innerHTML = "Find a # sign, followed by excatly 5 repeating digits, a period, and another digit";
    
  do {
    text1 = window.prompt("Enter the text(stop to exit)", "stop");
    if (text1 === "stop") { break; } 
    var re1 = /\w/g;      //\w – is the same as [a-zA-Z0-9_],
    var re2 = /[^0-9]/g;  //[^0-9] – any character except a digit, the same as \D.
    var re3 = /[A-Z][a-z]/g;
    var re4 = /\W\d{5}\.\d/g;
    if (re1.test(text1)) {
      document.getElementById("match1").innerHTML += "\n" + text1 + "It's a Match";
   } else {
      document.getElementById("nomatch1").innerHTML += "\n" + text1 + "It's not a Match";
   }           
    
    if (re2.test(text1)) {
      document.getElementById("match2").innerHTML += "\n" + text1 + "It's a Match";
    } else {
      document.getElementById("nomatch2").innerHTML += "\n" + text1 + "It's not a Match";
    }

    if (re3.test(text1)) {
      document.getElementById("match3").innerHTML += "\n" + text1 + "It's a Match";
    } else {
      document.getElementById("nomatch3").innerHTML += "\n" + text1 + "It's not a Match";
    }

    if (re4.test(text1)) {
      document.getElementById("match4").innerHTML += "\n" + text1 + "It's a Match";
    } else {
      document.getElementById("nomatch4").innerHTML += "\n" + text1 + "It's not a Match";
    }
    
  } while (text1 != "stop");    
};

matchRegularExpr();
&#13;
   <!DOCTYPE html>
    
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title>Lab12 Regular Expression</title>
     
    </head>
    <body bgcolor="#efe862">
        <p id="text1"></p>
        <p id="pattern1">  </p>
        <p id="match1"> </p>
        <p id="notmatch1"></p>
        <p id="pattern2"></p>
        <p id="match2"> </p>
        <p id="nomatch2"></p>
        <p id="pattern3"> </p>
        <p id="match3"></p>
        <p id="nomatch3"></p>
        <p id="pattern4"> </p>
        <p id="match4"></p>
        <p id="nomatch4"></p>
    </body>
    </html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我从您的问题中理解的是,您希望文本&#34;停止&#34;停止搜索而不是自己测试,对吧?好吧,你可以使用while循环轻松完成并进行一些修改:

 var text1 = prompt("Enter some text (stop to exit)", "stop")
 while(text1 != "test") {
    // do all the searches exactly as in your code but without assigning text1 at the begining
    // ...
    // reassign text1 at the end so that the condition is checked before the code runs
   text1 = prompt("Enter some text (stop to exit)", "stop")
 }