javascript 如何使用正则表达式匹配任意字母组合?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13546187/
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-10-26 19:06:55  来源:igfitidea点击:

How to match any combination of letters using regex?

javascriptjavahtmlregexperl

提问by John_Sheares

How can I match letters a,b,c once in any combination and varying length like this:

如何以任意组合和不同长度匹配字母 a,b,c 一次,如下所示:

The expression should match these cases:

该表达式应与以下情况匹配:

abc
bc
a
b
bca

but should not match these ones:

但不应与这些匹配:

abz
aab
cc
x

回答by ?mega

Use regex pattern

使用正则表达式

\b(?!(?:.\B)*(.)(?:\B.)*)[abc]+\b

You can use this pattern with any set and size, just replace [abc]with desired set...

您可以将此图案与任何套装和尺寸一起使用,只需替换[abc]为所需的套装...



Example:

示例

enter image description here

在此处输入图片说明

(above output is from myregextester)

(以上输出来自myregextester

回答by phant0m

^(?=([^a]*a?[^a]*)$)(?=([^b]*b?[^b]*)$)(?=([^c]*c?[^c]*)$)[abc]{1,3}$

This workswith lookaheads.

适用lookaheads

It includes this pattern in three variations: (?=([^a]*a?[^a]*)$)

它以三种变体形式包含此模式: (?=([^a]*a?[^a]*)$)

It says: There needs to be at most one afrom here (the beginning) until the end.

它说:a从这里(开始)到结束最多需要一个。

Combining lookaheadsand backreferences:

结合前瞻反向引用

^([abc])((?!)([abc])((?!)(?!)[abc])?)?$

回答by Alan Moore

Just to round out the collection:

只是为了完善集合:

^(?:([abc])(?!.*))+$

Want to handle a larger set of characters? No problem:

想要处理更大的字符集?没问题:

^(?:([abcdefgh])(?!.*))+$

EDIT:Apparently I misread the question; you're not validating individual strings like "abc"and "ba", you're trying to find whole-word matches in a larger string. Here's how I would do that:

编辑:显然我误读了这个问题;您不是在验证像"abc"and这样的单个字符串"ba",而是试图在更大的字符串中找到全字匹配。这是我将如何做到这一点:

\b(?:([abc])(?![abc]*))+\b

The tricky part is making sure the lookahead doesn't look beyond the end of the word that's currently being matched. For example, if I had left the lookahead as (?!.*\1), it would fail to match the abcin abc zabecause the lookahead would incorrectly flag the ain zaas a duplicate of the ain abc. Allowing the lookahead to look only at valid characters ([abc]*) keeps it on a sufficiently short leash. And if there are invalid characters in the current word, it's not the lookahead's job to spot them anyway.

棘手的部分是确保前瞻不会超出当前匹配的单词的末尾。例如,如果我离开了超前的(?!.*\1),它会失败,以配合abcabc za,因为先行将错误旗的aza作为的副本aabc。允许前瞻只查看有效字符 ( [abc]*) 使其保持足够短的皮带。如果当前单词中存在无效字符,则无论如何发现它们不是前瞻的工作。

(Thanks to Honest Abefor bringing this back to my attention.)

(感谢Honest Abe让我重新注意到这一点。)

回答by Kirill Polishchuk

Try this regex:

试试这个正则表达式:

^([abc])((?!)([abc]))?((?!(|))([abc]))?$

Check in regexpal

检查regexpal

回答by Bohemian

^(?=(.*a.*)?$)(?=(.*b.*)?$)(?=(.*c.*)?$)[abc]{,3}$

The anchored look-aheads limit the number of occurrences of each letter to one.

锚定前瞻将每个字母的出现次数限制为一个。

回答by Aimon Bustardo

I linked it in comment (this is sort of a dupe of How can I find repeated characters with a regex in Java?).. but to be more specific.. the regex:

我在评论中链接了它(这有点像如何在 Java 中找到带有正则表达式的重复字符?)..但更具体地说..正则表达式:

(\w)+

Will match any two or more of the same character. Negate that and you have your regex.

将匹配任意两个或多个相同字符。否定它,你就有了你的正则表达式。