扫码关注官方订阅号
javascript无法实现前后查找怎么办
比如这样hello<h2><span>world</a ></h2><h3>foo</h3> 如何匹配所有 <h([1-6])><\/h\1> 内的内容?
hello<h2><span>world</a ></h2><h3>foo</h3>
<h([1-6])><\/h\1>
业精于勤,荒于嬉;行成于思,毁于随。
不知道为什么,预览都是正确的,提交之后就乱了
简单的匹配,包含 <h?> 的:
<h?>
javascript"hello <h2><span>world</a ></h2> <h3>foo</h3> ".match(/<(h[1-6])>.*?<\/\1>/g) // 输出 // [" <h2><span>world</a ></h2> ", " <h3>foo</h3> "]
javascript
"hello <h2><span>world</a ></h2> <h3>foo</h3> ".match(/<(h[1-6])>.*?<\/\1>/g) // 输出 // [" <h2><span>world</a ></h2> ", " <h3>foo</h3> "]
复杂点的(只取中间那部分)
javascriptvar regex = /<(h[1-6])>(.*?)<\/\1>/g; var s = "hello <h2><span>world</a ></h2> <h3>foo</h3> "; do { var m = regex.exec(s, regex.lastIndex); console.log(m); } while(m); // 输出 // [" <h2><span>world</a ></h2> ", "h2", "<span>world</a >", index: 5, input: "hello <h2><span>world</a ></h2> <h3>foo</h3> "] // [" <h3>foo</h3> ", "h3", "foo", index: 30, input: "hello <h2><span>world</a ></h2> <h3>foo</h3> "] // null
var regex = /<(h[1-6])>(.*?)<\/\1>/g; var s = "hello <h2><span>world</a ></h2> <h3>foo</h3> "; do { var m = regex.exec(s, regex.lastIndex); console.log(m); } while(m); // 输出 // [" <h2><span>world</a ></h2> ", "h2", "<span>world</a >", index: 5, input: "hello <h2><span>world</a ></h2> <h3>foo</h3> "] // [" <h3>foo</h3> ", "h3", "foo", index: 30, input: "hello <h2><span>world</a ></h2> <h3>foo</h3> "] // null
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
简单的匹配,包含
<h?>的:复杂点的(只取中间那部分)