Javascript:在循环中使用提示对话框并针对每个正则表达式测试数据

时间:2017-10-13 23:18:32

标签: javascript

在我的javascript程序中,我创建了四个正则表达式并在循环中创建了提示,以便询问用户的输入。当用户输入任何数据时,它应该检查我创建的所有正则表达式并显示是否匹配"或"不匹配"每次测试后重复,直到用户输入"停止"并测试最后一次并退出。一个提示检查同样显示匹配。我的代码几乎准备就绪但由于一些错误,我的代码无法正常工作。任何人都可以纠正我吗?

代码:

<!DOCTYPE html>
<html>
<head>
    <title>lab12</title>

</head>
<body bgcolor="#efe862">
    <h3 id="txt1">  </h3>
    <p id= "text1"> </p>
    <p id="match1"></p>
    <p id="noMatch1"></p>

    <p id= "text2"> </p>
    <p id="match2"></p>
    <p id="noMatch2"></p>

    <p id= "text3"> </p>
    <p id="match3"></p>
    <p id="noMatch3"></p>

    <p id= "text4"> </p>
    <p id="match4"></p>
    <p id="noMatch4"></p>

        <script>
            myFunction();
        function myFunction() {

            var prom = prompt("Enter a text(stop to exit)");

            var patt1 = /\w/g;
            var patt2 = /\[^0-9]/g;  
            var patt3 = /[A-Z][a-z]\s/;
            var patt4 = /\W\d\d\d\d\d.?\d/;

            while(prom !== "stop"){
                var prom = prompt("Enter a text(stop to exit)");
            }

            do{

            document.getElementById("txt1").innerHTML = "The text is: " + prom;

            document.getElementById("text1").innerHTML = "Match a string that contains atleast one character in A-Z, a-z, 0-9 and _";

            if(patt1 || patt2 || patt3 || patt4.test(prom)){
            document.getElementById("match1").innerHTML = "It's match";
            }
            else{
                document.getElementById("noMatch1").innerHTML = "No match";
            }

            document.getElementById("text2").innerHTML = "Match a string that doesn't contain a number between 0 and 9";

            if(patt1 || patt2 || patt3 || patt4.test(prom)){
            document.getElementById("match2").innerHTML = "It's match";
            }
            else{
                document.getElementById("noMatch2").innerHTML = "No match";
            }

            document.getElementById("text3").innerHTML = "Search for an uppercase letter, followed by one or more lower case letters, followed by a space";

            if(patt1 || patt2 || patt3 || patt4.test(prom)){
            document.getElementById("match3").innerHTML = "It's match";
            }
            else{
                document.getElementById("noMatch3").innerHTML = "No match";
            }

            document.getElementById("text4").innerHTML = "Find a # sign, followed by exactly 5 repeating digits, a period and another digit";

            if(patt1 || patt2 || patt3 || patt4.test(prom)){
            document.getElementById("match4").innerHTML = "It's match";
            }
            else{
                document.getElementById("noMatch4").innerHTML = "No match";
            }

            }
        }
        </script>

</body>
</html>

预期产出:

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

您正在尝试使用do/while loop,但方式错误。试试这样:

var prom = prompt("Enter a text(stop to exit)");

while (prom !== "stop") {
    // rest of the function...
    prom = prompt("Enter a text(stop to exit)");
}

请注意,只有一个prom变量声明(var prom = ...),您无法在代码中再次执行此操作,因为这会声明新变量(在您的示例中位于不同范围内),因此原始prom变量值将保持不变。

此外,您正在错误地检查正则表达式,您无法执行if (patt1 || patt2 || patt3 || patt4.test(prom)) {,这将检查变量patt1是否为真(这是定义为正则表达式)。您想为所有正则表达式运行re.test()

if (patt1.test(prom) || patt2.test(prom) || patt3.test(prom) || patt4.test(prom)) {

这样的事情

<!DOCTYPE html>
<html>
<head><title>lab12</title></head>
<body bgcolor="#efe862">
<h3 id="txt1"></h3>
<p id="text1"></p>
<p id="match1"></p>
<p id="noMatch1"></p>

<p id="text2"></p>
<p id="match2"></p>
<p id="noMatch2"></p>

<p id="text3"></p>
<p id="match3"></p>
<p id="noMatch3"></p>

<p id="text4"></p>
<p id="match4"></p>
<p id="noMatch4"></p>

<script>
    myFunction();

    function myFunction() {
        var patt1 = /\w/g;
        var patt2 = /\[^0-9]/g;
        var patt3 = /[A-Z][a-z]\s/;
        var patt4 = /\W\d\d\d\d\d.?\d/;
        var prom = prompt("Enter a text(stop to exit)");

        while (prom !== 'stop') {
            if (prom === "stop") {
                return;
            }

            document.getElementById("txt1").innerHTML = "The text is: " + prom;
            document.getElementById("text1").innerHTML = "Match a string that contains atleast one character in A-Z, a-z, 0-9 and _";

            if (patt1.test(prom)) {
                document.getElementById("match1").innerHTML = "It's match";
            } else {
                document.getElementById("noMatch1").innerHTML = "No match";
            }

            document.getElementById("text2").innerHTML = "Match a string that doesn't contain a number between 0 and 9";

            if (patt2.test(prom)) {
                document.getElementById("match2").innerHTML = "It's match";
            } else {
                document.getElementById("noMatch2").innerHTML = "No match";
            }

            document.getElementById("text3").innerHTML = "Search for an uppercase letter, followed by one or more lower case letters, followed by a space";

            if (patt3.test(prom)) {
                document.getElementById("match3").innerHTML = "It's match";
            } else {
                document.getElementById("noMatch3").innerHTML = "No match";
            }

            document.getElementById("text4").innerHTML = "Find a # sign, followed by exactly 5 repeating digits, a period and another digit";

            if (patt4.test(prom)) {
                document.getElementById("match4").innerHTML = "It's match";
            } else {
                document.getElementById("noMatch4").innerHTML = "No match";
            }

            prom = prompt("Enter a text(stop to exit)");
        }
    }
</script>

</body>
</html>