问题描述
我对此进行了相当多的研究,但找不到如何将嵌套的 html 标签 与 属性匹配的工作示例.我知道可以匹配没有属性的平衡/嵌套最里面的标签(例如,正则表达式和将是 #<div[^>]*>(?:(?> [^<]+ ) |<(?!div[^>]*>))*?</div>#x).
但是,我希望看到一个正则表达式模式,它可以找到带有属性的 html 标记对.
例子:基本上应该匹配
<div class="aaa">**<div class="aaa">** <div></div>**</div>** </div>而不是
<div class="aaa">**<div class="aaa">** <div>**</div>** </div></div>有人有什么想法吗?
出于测试目的,我们可以使用:http://www.lumadis.be/regex/test_regex.php
<小时>PS.Steven 在他的博客中提到了一个解决方案(实际上是在评论中),但它不起作用
http://blog.stevenlevithan.com/archives/match-innermost-html元素
$regex = '/ 解决方案 匹配 <div> &</div> 标签,加上它们的属性 &内容:
#<div(?:(?!(<div|</div>)).)*</div>#s
这里的关键是 (?:(?!STRING).)* 是字符串,就像 [^CHAR]* 是字符一样.
来源:https://stackoverflow.com/a/6996274
<小时>PHP 中的示例:
$match) {回声************".
".$匹配."
";}
输出:
************<div id="3">在 3</div>************<div id="5">在 5</div>
I researched this quite a bit, but couldn't find a working example how to match nested html tags with attributes. I know it is possible to match balanced/nested innermost tags without attributes (for example a regex for and would be #<div[^>]*>(?:(?> [^<]+ ) |<(?!div[^>]*>))*?</div>#x).
However, I would like to see a regex pattern that finds an html tag pair with attributes.
Example: It basically should match
<div class="aaa"> **<div class="aaa">** <div> <div> </div> **</div>** </div>
and not
<div class="aaa"> **<div class="aaa">** <div> <div> **</div>** </div> </div>
Anybody has some ideas?
For testing purposes we could use: http://www.lumadis.be/regex/test_regex.php
PS. Steven mentioned a solution in his blog (actually in a comment), but it doesn't work
http://blog.stevenlevithan.com/archives/match-innermost-html-element
$regex = '/<div[^>]+?ids*=s*"MyID"[^>]*>(?:((?:[^<]++|<(?!/?div[^>]*>))+)|(<div[^>]*>(?>(?1)|(?2))*</div>))?</div>/i';
解决方案 Matching innermost matching pairs of <div> & </div> tags, plus their attributes & content:
#<div(?:(?!(<div|</div>)).)*</div>#s
The key here is that (?:(?!STRING).)* is to strings as [^CHAR]* is to characters.
Credit: https://stackoverflow.com/a/6996274
Example in PHP:
<?php
$text = <<<'EOD'
<div id="1">
in 1
<div id="2">
in 2
<div id="3">
in 3
</div>
</div>
</div>
<div id="4">
in 4
<div id="5">
in 5
</div>
</div>
EOD;
$matches = array();
preg_match_all('#<div(?:(?!(<div|</div>)).)*</div>#s', $text, $matches);
foreach ($matches[0] as $index => $match) {
echo "************" . "
" . $match . "
";
}
Outputs:
************
<div id="3">
in 3
</div>
************
<div id="5">
in 5
</div>
这篇关于嵌套标签的正则表达式(最里面使它更容易)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14
前端开发问题
156
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24
前端开发问题
271
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18
前端开发问题
125
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20
前端开发问题
5
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20
前端开发问题
13
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)...
2024-04-20
前端开发问题
6
热门文章
1错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()
2vue中yarn install报错:info There appears to be trouble with you
3为什么 Chrome(在 Electron 内部)会突然重定向到 chrome-error://chromewebdat
4“aria-hidden 元素不包含可聚焦元素"显示模态时的问题
5使用选择器在 CSS 中选择元素的前一个兄弟
6js报错:Uncaught SyntaxError: Unexpected string
7layui怎么刷新当前页面?
8将模式设置为“no-cors"时使用 fetch 访问 API 时出错
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)