如题:求一正则:匹配字符串中id为xxx的div
<div id="xxx"><span>123</span></div>
html结构是后台返回的吗?
const html = `<div id="xxx"><span>123</span></div><div class="yyy" id="yyy"><span>1234</span></div><div id="zzz" class="xxx"><span>12345</span></div><div id=fff><span>123456</span></div>
`
var test = html.match(/.*?(id="xxx"|id=xxx)[\s\S].*?(?<=<\/div>)/g);
const html = `
<div id="xxx"><span>123</span></div>
<div class="xxx" id="xxx"><span>1234</span></div>
<div id="xxx" class="xxx"><span>12345</span></div>
<div id=xxx><span>123456</span></div>
`
html.match(/(?<=<div[\s\S]*?(id="xxx"|id=xxx)[^>]*?>)[\s\S]*?(?=<\/div>)/g); // 不匹配div
html.match(/<div[\s\S]*?(id="xxx"|id=xxx)[^>]*?>([\s\S]*?)<\/div>/g) // 匹配div
const el = document.createElement('div');
el.innerHTML = `your html string`;
document.body.appendChild(el);
const $xxx = document.getElementById('xxx');
const html = $xxx.innerHTML;
// todo
10 回答11.1k 阅读
6 回答3k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
3 回答5.1k 阅读✓ 已解决
3 回答1.8k 阅读✓ 已解决
JS正则干不了这个事情。
即使用正则不能匹配开闭标签