使用正则表达式提取元标记

我需要从我正在使用的字符串中提取元标记,\<meta[\s\S]*?\>但除此之外,如果元中有一些忽略(或 someIgnore)属性,我想忽略它。


<meta property="position" content="1" someIgnore ignore="metaextract"/>.

这是我的示例函数。



function parseMetas(locals) {

    var str = locals.body, regex = /\<meta[\s\S]*?\>/g;

    if (regex.test(str)) {

        locals.body = str.replace(regex, '');

        locals.meta = str.match(regex).join('\n');

    }

}


holdtom
浏览 72回答 1
1回答

饮歌长啸

您可以negative lookahead在正则表达式中使用。function parseMetas(locals) {&nbsp; &nbsp; var str = locals.body,&nbsp;&nbsp; &nbsp; let regex = /<meta(?!.*?(ignore|someIgnore))[\s\S]*?\/?>/g;&nbsp; &nbsp; if (regex.test(str)) {&nbsp; &nbsp; &nbsp; &nbsp; locals.body = str.replace(regex, '');&nbsp; &nbsp; &nbsp; &nbsp; locals.meta = str.match(regex).join('\n');&nbsp; &nbsp; }}演示:let regex = /<meta(?!.*(ignore|someIgnore))[\s\S]*?\/>/g;let input = `&nbsp; &nbsp; <meta property="position" content="1" someIgnore ignore="metaextract"/>,&nbsp; &nbsp; <meta property="position" content="1" ignore="metaextract"/>,&nbsp; &nbsp; <meta property="position" content="1"/>,&nbsp; &nbsp; <meta property="position" content="1" someIgnore />,&nbsp; &nbsp; <meta name="description" content="type_your_description_here"/>,&nbsp; &nbsp; <meta charset="utf-8"/>'`;console.log(input.match(regex));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript