用于匹配仅由字母列表构成的单词的正则表达式

RegEx for matching words only formed with a list of letters

给定一组单词,我需要知道哪些单词仅由一组字母组成。这个词的字母不能超过允许的数量,即使这个字母是验证集的一部分。

示例:

Char set: a, a, ã, c, e, l, m, m, m, o, o, o, o, t (fixed set)

Words set: mom, ace, to, toooo, ten, all, aaa (variable set)

结果:

mom = true
ace = true
to = true
toooo = true
ten = false (n is not in the set)
all = false (there is only 1 L in the set)
aaa = false (theres is only 2 A in the set)

如何在Javascript中生成这个正则表达式? (区分大小写不是问题)。

我试过这段代码没有成功:

var str = "ten"
var patt = new RegExp("^[a, a, ã, c, e, l, m, m, m, o, o, o, o, t]*");
console.log(patt.test(str));

虽然我觉得这个任务更适合编写一些代码而不是使用正则表达式。但我认为应该可行的一种方法是使用负面展望。

以您的字符集为例,您允许的单词可以有以下字母,并且不超过列表中出现的数量。

a, a, ã, c, e, l, m, m, m, o, o, o, o, t

我们可以编写以下正则表达式,它使用否定前瞻来丢弃包含的字符数超过上面为每个字符设置的允许字符数的字符串,最后使用允许的字符集从 1 到 N 个字符捕获单词,其中N是字符总数。

^(?!([^a]*a){3})(?!([^ã]*ã){2})(?!([^c]*c){2})(?!([^e]*e){2})(?!([^l]*l){2})(?!([^m]*m){4})(?!([^o]*o){5})(?!([^t]*t){2})[aãcelmot]{1,14}$

解释:

  • ^ - 字符串开始
  • (?!([^a]*a){3}) - 如果字符串中 a 的数量为 3 或更多,因为集合中 a 的总数仅为 2.
  • (?!([^ã]*ã){2}) - 类似地,如果字符串中 ã 的数量为 2 或更多,则此否定前瞻将拒绝输入,因为 ã 的集合总数仅为一个。
  • 所有字符依此类推
  • [aãcelmot]{1,14} - 这个字符集捕获至少 1 到最多 14 个允许的字符,尽管我们也可以简单地写 + 因为已经使用否定前瞻检查了允许字符的最大数量.
  • $ - 字符串结束

JS 代码演示,

const arr = ['mom','ace','to','toooo','ten','all','aaa']

arr.forEach(x => console.log(x + " --> " +/^(?!([^a]*a){3})(?!([^ã]*ã){2})(?!([^c]*c){2})(?!([^e]*e){2})(?!([^l]*l){2})(?!([^m]*m){4})(?!([^o]*o){5})(?!([^t]*t){2})[aãcelmot]{1,14}$/.test(x)))