ftp
4 سال پیش توسط ftp مطرح شد
1 پاسخ

عبارت های باقاعده یا regular expression

@milad
@ali.bayat
@kianlatifi
@endworld
من میخوام با preg,matchall تگ دلخواه <a> رو بیرون بکشم یعنی در این کد ما سه تا تگ aداریم من میخوام تک a دوم رو بگیرم یا تگ a سوم رو بگیرم چطوری میتونم این کار رو انجام بدم میشه راهنمایی کنید

<p>
<a href="\&quot;\/download\/?url=https%3A%2F%2Fwww.youtube.com%2Fapi%2Ftimedtext%3Fv%3DgAkwW2tuIqE%26asr_langs%3Dde%2Cen%2Ces%2Cfr%2Cit%2Cja%2Cko%2Cnl%2Cpt%2Cru%26caps%3Dasr%26xorp%3Dtrue%26xoaf%3D5%26hl%3Den%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1604065133%26sparams%3Dip%2Cipbits%2Cexpire%2Cv%2Casr_langs%2Ccaps%2Cxorp%2Cxoaf%26signature%3D4DE2105D3F9F340F7DE09F57C246E90A0495759D.BA4AD77A037B8F29EBEF1D0B5AC1CAB508D972BD%26key%3Dyt8%26kind%3Dasr%26lang%3Den&amp;title=Learn+Docker+in+7+Easy+Steps+-+Full+Beginner%27s+Tutorial\&quot;">
</a>
<a href="\&quot;\/download\/?url=https%3A%2F%2Fwww.youtube.com%2Fapi%2Ftimedtext%3Fv%3DgAkwW2tuIqE%26asr_langs%3Dde%2Cen%2Ces%2Cfr%2Cit%2Cja%2Cko%2Cnl%2Cpt%2Cru%26caps%3Dasr%26xorp%3Dtrue%26xoaf%3D5%26hl%3Den%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1604065133%26sparams%3Dip%2Cipbits%2Cexpire%2Cv%2Casr_langs%2Ccaps%2Cxorp%2Cxoaf%26signature%3D4DE2105D3F9F340F7DE09F57C246E90A0495759D.BA4AD77A037B8F29EBEF1D0B5AC1CAB508D972BD%26key%3Dyt8%26kind%3Dasr%26lang%3Den%26tlang%3Daf&amp;title=Learn+Docker+in+7+Easy+Steps+-+Full+Beginner%27s+Tutorial\&quot;"><b>Download&lt;\/b&gt;&lt;\/a&gt;&nbsp;&nbsp;Afrikaans<br\>&lt;\/p&gt;\n</br\></b>
</a>
<a href="\&quot;\/download\/?url=https%3A%2F%2Fwww.youtube.com%2Fapi%2Ftimedtext%3Fv%3DgAkwW2tuIqE%26asr_langs%3Dde%2Cen%2Ces%2Cfr%2Cit%2Cja%2Cko%2Cnl%2Cpt%2Cru%26caps%3Dasr%26xorp%3Dtrue%26xoaf%3D5%26hl%3Den%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1604065133%26sparams%3Dip%2Cipbits%2Cexpire%2Cv%2Casr_langs%2Ccaps%2Cxorp%2Cxoaf%26signature%3D4DE2105D3F9F340F7DE09F57C246E90A0495759D.BA4AD77A037B8F29EBEF1D0B5AC1CAB508D972BD%26key%3Dyt8%26kind%3Dasr%26lang%3Den%26tlang%3Daf&amp;title=Learn+Docker+in+7+Easy+Steps+-+Full+Beginner%27s+Tutorial\&quot;"><b>Download&lt;\/b&gt;&lt;\/a&gt;&nbsp;&nbsp;Afrikaans<br\>&lt;\/p&gt;\n</br\></b>
</a>
</p>

ثبت پرسش جدید
میلاد-م
تخصص : توسعه‌دهنده رابط کاربری - Fron...
@milad 4 سال پیش مطرح شد
0

این باید پاسخ سوالتون رو نشون بده (کدها در جاوااسکریپت):

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>regex-nth-occurence</title>
    <style>

        #regexSel {
            padding-bottom: 12px;
            border-bottom: 1px solid silver
        }

        .btn {
            display: inline-block;          
            padding: 8px 12px;
            margin-bottom: 10px;
            font-family: inherit;
            font-size: 18px;
            text-align: center;
            text-decoration: none;
            background-color: darkcyan;
            color: white;
            border: none;
            border-radius: 4px;
            transition: .2s;
            cursor: pointer;
        }

        .btn:hover {
            filter: brightness(120%);
        }

    </style>
</head>
<body>
    <ul id="regexSel">
        <li><a href="http://exam1.com" class="btn">exam1</a></li>
        <li><a href="http://exam2.com" class="btn">exam2</a></li>
        <li><a href="http://exam3.com" class="btn">exam3</a></li>
        <li><a href="http://exam4.com" class="btn">exam4</a></li>
        <li><a href="http://exam5.com" class="btn">exam5</a></li>
        <li><a href="http://exam6.com" class="btn">exam6</a></li>
        <li><a href="http://exam7.com" class="btn">exam7</a></li>
    </ul>

    <p>
        answer for 3th occurence: <span id='answer'></span>
    </p>

    <script>
        let str = document.querySelector('#regexSel').innerHTML;

        // ?: for Non-capturing groups in regular expressions
        // {2}: change number for (n+1)th occurence
        let regexp = /(?:[\s\S]*?<a[^>]*>([^<]+)<\/a>[\s\S]*?){2}(<a[^>]*>([^<]+)<\/a>)/;

        let result = str.match(regexp);

        document.querySelector('#answer').innerHTML = result[2];

        console.log( result.length );
        console.log( result[0] ); 
        console.log( result[1] ); 
        console.log( result[2] );   // answer
        console.log( result[3] );
        console.log( result[4] );

    </script>
</body>
</html>

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام