Javascript 如何匹配字符 '<' 后面没有('a' 或 'em' 或 'strong')?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2706745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:34:05  来源:igfitidea点击:

How to match the character '<' not followed by ('a' or 'em' or 'strong')?

javascriptregex

提问by Kyle

How would I make a regular expression to match the character <not followed by (aor emor strong)

我将如何做一个正则表达式匹配的字符<后面没有(aemstrong

So <helloand <stringwould match, but <strongwouldn't.

所以<hello<string会匹配,但<strong不会。

回答by Andrew Hare

Try this:

尝试这个:

<(?!a|em|strong)

回答by cletus

You use a negative lookahead, the simplest form for which is (for this problem):

您使用了一个否定的 lookahead,最简单的形式是(对于这个问题):

<(?!a|em|strong)

The one issue with that is that it will ignore <applet>. A way to deal with that is by using \b, which is a zero-width expression (meaning it captures none of the input) that matches a word to non-word or non-word to word transition. Word characters are [0-9a-zA-Z_]. So:

一个问题是它会忽略<applet>. 一种处理方法是使用\b,它是一个零宽度表达式(意味着它不捕获任何输入),它匹配单词到非单词或非单词到单词的转换。Word 字符是[0-9a-zA-Z_]. 所以:

<(?!(a|em|strong)\b)

回答by Antal Spector-Zabusky

If your regex engine supports it, use a negative lookahead assertion: this looks ahead in the string, and succeeds if it wouldn't match; however, it doesn't consume any input. Thus, you want /<(?!(?:a|em|strong)\b)/: match a <, then succeed if there isn't an a, em, or strongfollowed by a word break, \b.

如果您的正则表达式引擎支持它,请使用否定前瞻断言:这会在字符串中进行前瞻,如果不匹配则成功;但是,它不消耗任何输入。因此,你想 /<(?!(?:a|em|strong)\b)/:匹配A <,然后成功,如果没有一个aem或者strong后面一个字突破,\b

回答by WoodrowShigeru

Although Andrew's answer is clearly superior, before, I also got it to work with [^(?:a|em|strong)].

尽管安德鲁的回答显然更胜一筹,但在此之前,我也将其用于[^(?:a|em|strong)].

回答by SwiftNinjaPro

function strip_tags(str, keep){
    if(keep && Array.isArray(keep)){keep = '|'+keep.join('|');}else if(keep){keep = '|'+keep;}else{keep = '';}
    return str.replace(new RegExp('<\/?(?![^A-Za-z0-9_\-]'+keep+').*?>', 'g'), '');
}

usage:

用法:

strip_tags('<html><a href="a">a</a> <strong>strong text</strong> and <em>italic text</em></html>', ['strong', 'em']);
//output: a <strong>strong text</strong> and <em>italic text</em>

I would also recommend you strip parameters from the tags you keep

我还建议您从保留的标签中去除参数

function strip_params(str){
    return str.replace(/<((?:[A-Za-z0-9_\-])).*?>/g, '<>');
}