【发布时间】:2016-10-20 21:30:16
【问题描述】:
创建动态正则表达式以匹配一组不同的字符(运行时提供的字符及其顺序)的最佳方法是什么。
character set: abcd
character format: ??j? (question mark represents a a character from character set)
例子
abjd = match
bdja = match
dbja = match
ab = no match
aajd = no match
abjdd = no match
abj = no match
我创建了一个正则表达式构建器(在 js 中),如下所示:
// characters are the character set
// wordFormat is the character format
// replace(str, search, replacement) replaces search in str with replacement
var chars = "[" + characters + "]{1}";
var afterSpecialConversion = replace(wordFormat, "?", chars);
var myRegex = new RegExp("^" + afterSpecialConversion + "$", "gi");
不幸的是,这并没有达到结果,因为它不考虑重复项。我考虑过使用匹配组来避免重复,但是我不知道如何从集合的其余部分中否定已经存在的字符组。
还给定字符集aabcd 现在a 可以存在两次。有什么建议么?
【问题讨论】:
标签: javascript regex text dynamic match