【发布时间】:2019-09-25 10:11:35
【问题描述】:
给定一组单词,我需要知道哪些单词仅由一组字母组成。这个词不能有超过允许的字母,即使这个字母是验证集的一部分。
例子:
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));
【问题讨论】:
-
这不是代码编写服务。向我们展示您到目前为止的想法。然后也许我们可以帮助修复该代码
-
我用里面的代码编辑了我的帖子。
-
如果只有 1 个
o这将是toooo = false? -
@G.aziz,是的。如果有 5 个 O,这将是 toooooo = true,但是, toooooo = false。
-
我不认为这个问题可以使用正则表达式来解决。正则表达式是一个没有内存的有限状态机,因此无法记住一个字符在一个序列中匹配了多少次
标签: javascript regex regex-lookarounds regex-group regex-greedy