首页 > javascript无法实现前后查找怎么办

javascript无法实现前后查找怎么办

javascript无法实现前后查找怎么办

比如这样
hello<h2><span>world</a ></h2><h3>foo</h3> 如何匹配所有 <h([1-6])><\/h\1> 内的内容?


不知道为什么,预览都是正确的,提交之后就乱了

简单的匹配,包含 <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>

"]

复杂点的(只取中间那部分)

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
【热门文章】
【热门文章】