正则表达式:在 html 中查找不包含方括号和文本的链接,例如。

情况1:

示例 html:<a href="https://www.jessussaveme.com/saveme/c-from.html?random[for_god_sake_save_me]=anyonethere&no=fr&lang=fr">Test</a>

预期输出:

https://www.jessussaveme.com/saveme/c-from.html?random[for_god_sake_save_me]=anyonethere&no=fr&lang=fr

案例二:

示例 html:<a href="https://www.jessussaveme.com/saveme/c-from.html?random[]=anyonethere&no=fr&lang=fr">Test</a>

预期输出:没有。链接不应包含空方括号 []

案例3:

示例 html:<a href="https://www.jessussaveme.com/saveme/c-from.html?random=anyonethere&no=fr&lang=fr">Test</a>

预期输出:https://www.jessussaveme.com/saveme/c-from.html?random=anyonethere&no=fr&lang=fr

应该选择哪些链接: 1. 不包含任何方括号 '[]' 的 链接 2. 包含非空方括号 '[Some_random_text]' 的链接

不应选择 的链接:包含空方括号 [] 的链接。


开满天机
浏览 132回答 2
2回答

繁花如伊

您可以使用 jQuery 而不是正则表达式:$("a").each(function(index) { // iterates all <a> elements&nbsp; console.log($(this).attr('href').includes('[]') ? '' : $(this).attr('href')); // check if contain "[]" or not.});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><a href="https://www.jessussaveme.com/saveme/c-from.html?reges[for_god_sake_save_me]=anyonethere&no=fr&lang=fr">Test</a><a href="https://www.jessussaveme.com/saveme/c-from.html?reges[]=anyonethere&no=fr&lang=fr">Test</a><a href="https://www.jessussaveme.com/saveme/c-from.html?random=anyonethere&no=fr&lang=fr">Test</a>除非您可以从a href 您那里获取文本,否则不应使用正则表达式来解析.由于您已经说过您使用 PHP,您可以尝试以下方法来提取 URL:$html = '<a href="https://www.jessussaveme.com/saveme/c-from.html?reges[for_god_sake_save_me]=anyonethere&no=fr&lang=fr">Test</a>&nbsp; &nbsp; <a href="https://www.jessussaveme.com/saveme/c-from.html?reges[]=anyonethere&no=fr&lang=fr">Test</a>&nbsp; &nbsp; <a href="https://www.jessussaveme.com/saveme/c-from.html?random=anyonethere&no=fr&lang=fr">Test</a>';$hrefs = array();$dom = new DOMDocument();$dom->loadHTML($html);$tags = $dom->getElementsByTagName('a');foreach ($tags as $tag) {&nbsp; &nbsp; &nbsp; &nbsp;$hrefs[] =&nbsp; $tag->getAttribute('href');}并检查是否包含空括号:foreach($hrefs as $a)&nbsp;{&nbsp; &nbsp; if (strpos($a, '[]') == false) {&nbsp; &nbsp; &nbsp; &nbsp; echo 'true'; // doesn't contain empty bracket&nbsp; &nbsp; }}

牧羊人nacy

这个有效:<\S.*?=\"(.*reges\[\w+\].*)\">.*>你可以看到它在这里工作。它只匹配组 1 中的第一个标签,并且在 [ ] 为空时在第二种情况下不返回任何内容。https://regex101.com/r/cdvVnP/1编辑:对于第三种情况,它应该类似于以下内容:if( !str.contains("reges[")){&nbsp; //passed() -pick up tat link as string doesnt contain reges[] or reges [some text]}else{//match with <\S.*?=\"(.*reges\[\w+\].*)\">.*>// if you find match then pickup that link from group 1}
打开App,查看更多内容
随时随地看视频慕课网APP