如何使用regexp捕获第一次出现的预定义输入?

时间:2017-12-31 08:55:10

标签: regex

我正在学习regexp.my regexp是:

<!DOCTYPE html>
<html>

<head>
    <title>Masters of Engineering</title>
    <link rel="stylesheet" type="text/css" href="C:\Users\Zach\Desktop\Coding\Robotics Site\css\Main.css">
    <link rel="icon" href="img/Corner Logo.ico">
    <meta charset="utf-8">
</head>

<body>
    <div id="Bar"></div>
    <script>
    function openNav() {
        document.getElementById("myNav").style.width = "100%";
    }

    function closeNav() {
        document.getElementById("myNav").style.width = "0%";
    }
    </script>
    <span style="font-size:30px;cursor:pointer; display: inline-block; margin-right: 15px" onmouseenter="openNav()" onmouseleave="closeNav()">&#9776;open</span>
    <div id="myNav" class="overlay" onmouseleave="closeNav()" onmouseenter="openNav()">
        <div class="overlay-content">
            <a href="#">About</a>
            <a href="#">Services</a>
            <a href="#">Clients</a>
            <a href="#">Contact</a>
        </div>
    </div>
    <span style="font-size:30px;cursor:pointer; display: inline-block;" onmouseenter="openNav()" onmouseleave="closeNav()">&#9776;open</span>
    <div id="myNav" class="overlay" onmouseleave="closeNav()" onmouseenter="openNav()">
        <div class="overlay-content">
            <a href="#">About</a>
            <a href="#">Services</a>
            <a href="#">Clients</a>
            <a href="#">Contact</a>
        </div>
    </div>
    <div class='slideshow'>
        <div class="slides">
            <img class="mySlides fade" src="https://wallpaperbrowse.com/media/images/3848765-wallpaper-images-download.jpg" height="auto" width="auto">
            <img class="mySlides fade" src="https://avante.biz/wp-content/uploads/Wallpaper-Images/Wallpaper-Images-005.jpg" height=auto width=auto>
            <img class="mySlides fade" src='https://svs.gsfc.nasa.gov/vis/a010000/a012000/a012005/Swift_Tidal_Disruption_2_Still.jpg' height="auto" width="auto">
        </div>
        <div class='buttons'>
            <a class="prev" onclick="plusDivs(-1); 
            clearInterval(myTimer); 
            myTimer = setInterval(carousel, 3500);">&#10094;
            </a>
            <a class="next" onclick="plusDivs(1); 
            clearInterval(myTimer);
            myTimer = setInterval(carousel, 3500);">&#10095;
            </a>
        </div>
    </div>
    <script>
    var myIndex = 0;
    var myTimer = setInterval(carousel, 3500);
    carousel();

    function carousel() {
        var i;
        var x = document.getElementsByClassName("mySlides");
        for (i = 0; i < x.length; i++) {
            x[i].style.display = "none";
        }

        myIndex++;
        if (myIndex > x.length) { myIndex = 1 }
        x[myIndex - 1].style.display = "block ";
    }
    myTimer; //change image every 3.5 seconds
    </script>
    <script>
    var slideIndex = 1;
    showDivs(slideIndex);

    function plusDivs(n) {
        showDivs(slideIndex += n);
    }

    function showDivs(n) {
        var i;
        var x = document.getElementsByClassName("mySlides");
        if (n > x.length) { slideIndex = 1 }
        if (n < 1) { slideIndex = x.length }
        for (i = 0; i < x.length; i++) {
            x[i].style.display = "none";
        }
        x[slideIndex - 1].style.display = "block ";
    }
    </script>
    </div>
    <div class='footer'>
        <img src='http://www.mastersny.org/uploaded/template/default/images/common/bottom-logo.png'>
        <p>49 Clinton Avenue</p>
        <p>Dobbs Ferry, NY 10522</p>
        <p>(914) 479-6400</p>
    </div>
</body>

我的示例数据是:/^([0-5][0-9]|60):/g

一切都很好。现在我想在之前提取第一个:。即:60但是当我尝试使用1美元时。它返回60:40:60 Lua。任何的想法?

1 个答案:

答案 0 :(得分:1)

那个怎么样:

^(?:[0-5][0-9]|60)(?=:)

Demo

或者按照您的验证方式,您可以尝试:

^(?:[0-5][0-9]|60)(?=:\d{2}:\d{2}.*)

说明:

  1. ^(?:[0-5][0-9]|60) - 与您的相同
  2. (?=:\d{2}:\d{2}.*) - 积极前瞻以确保前一个模式后面跟着2位冒号2 digt,然后是其余的零或更长的字符串。
  3. Demo 2

    由于它似乎没有根据您的需要提供服务,因此有一个解决方案可以捕获3组,其中组1以您想要的数字开头:

    const regex = /^([0-5][0-9]|60):([0-5][0-9]|60):([0-5][0-9]|60).*$/gm;
    const str = `60:40:60 Lua`;
    let m;
    
    if ((m = regex.exec(str)) !== null) {
    console.log(m[1]);
    console.log(m[2]);
    console.log(m[3]);
    }