1 回答
TA贡献1951条经验 获得超3个赞
您可以negative lookahead在正则表达式中使用。
function parseMetas(locals) {
var str = locals.body,
let regex = /<meta(?!.*?(ignore|someIgnore))[\s\S]*?\/?>/g;
if (regex.test(str)) {
locals.body = str.replace(regex, '');
locals.meta = str.match(regex).join('\n');
}
}
演示:
let regex = /<meta(?!.*(ignore|someIgnore))[\s\S]*?\/>/g;
let input = `
<meta property="position" content="1" someIgnore ignore="metaextract"/>,
<meta property="position" content="1" ignore="metaextract"/>,
<meta property="position" content="1"/>,
<meta property="position" content="1" someIgnore />,
<meta name="description" content="type_your_description_here"/>,
<meta charset="utf-8"/>'
`;
console.log(input.match(regex));
添加回答
举报