正则表达式找到4个字符的模式

时间:2021-04-03 21:52:35

I'm confused how to do this. I have an array of strings that match this pattern:

我很困惑如何做到这一点。我有一个匹配此模式的字符串数组:

AABC,
ABBC,
ABCC,
AAAB,
ABBB

I'm hoping to use regex to figure out if a particular string matches one of these patterns. The strings themselves do not actually consist of A, B, or C. This is just to show how the pattern looks like. For example: Pattern AAAB should not equal BAAA.

我希望使用正则表达式来确定特定字符串是否与这些模式之一匹配。字符串本身实际上不包含A,B或C.这只是为了显示模式的外观。例如:模式AAAB不应该等于BAAA。

Can you help?

你能帮我吗?

Edit - For example:

编辑 - 例如:

['TTES', 'TEES', 'TESS', 'TTTE', 'TEEE']

array[0] should match AABC,

array [0]应匹配AABC,

array[1] should match ABBC,

array [1]应匹配ABBC,

array[2] should match ABCC,

array [2]应匹配ABCC,

array[3] should match AAAB,

array [3]应匹配AAAB,

array[4] should match ABBB,

array [4]应该与ABBB匹配,

1 个解决方案

#1


2  

You could generate RegExp from your patterns this way:

您可以通过以下方式从模式生成RegExp:

var stringToPattern = function (string) {
    var regexString = string.split('').map(function (char, position, string) {

        var prevIndex = string.indexOf(char);

        return prevIndex < position ? '(\\' + (prevIndex + 1) + ')' : '(.)';

    }).join('');

    return new RegExp(regexString);
};

The function generates a regular expression based on the input string:

该函数根据输入字符串生成正则表达式:

  • AABC to /(.)(\1)(.)(.)/
  • AABC到/(.)(\1)(.)(.)/

  • ABBC to /(.)(.)(\2)(.)/
  • ABBC到/(.)(.)(\2)(.)/

  • etc...

Examples:

stringToPattern('AABC').test('TTES'); // true
stringToPattern('AABC').test('TEES'); // false

stringToPattern('ABBC').test('TEES'); // true
stringToPattern('ABBC').test('TTES'); // false

update

var getMatchingPatterns = function (string) {
    var patterns = [
        'AABC',
        'ABBC',
        'ABCC',
        'AAAB',
        'ABBB'
    ];
    return patterns.filter(function (pattern) {
        return stringToPattern(pattern).test(string);
    });
};

examples:

getMatchingPatterns('ttob'); // ['AABC']
getMatchingPatterns('ottb'); // ['ABBC']

#1


2  

You could generate RegExp from your patterns this way:

您可以通过以下方式从模式生成RegExp:

var stringToPattern = function (string) {
    var regexString = string.split('').map(function (char, position, string) {

        var prevIndex = string.indexOf(char);

        return prevIndex < position ? '(\\' + (prevIndex + 1) + ')' : '(.)';

    }).join('');

    return new RegExp(regexString);
};

The function generates a regular expression based on the input string:

该函数根据输入字符串生成正则表达式:

  • AABC to /(.)(\1)(.)(.)/
  • AABC到/(.)(\1)(.)(.)/

  • ABBC to /(.)(.)(\2)(.)/
  • ABBC到/(.)(.)(\2)(.)/

  • etc...

Examples:

stringToPattern('AABC').test('TTES'); // true
stringToPattern('AABC').test('TEES'); // false

stringToPattern('ABBC').test('TEES'); // true
stringToPattern('ABBC').test('TTES'); // false

update

var getMatchingPatterns = function (string) {
    var patterns = [
        'AABC',
        'ABBC',
        'ABCC',
        'AAAB',
        'ABBB'
    ];
    return patterns.filter(function (pattern) {
        return stringToPattern(pattern).test(string);
    });
};

examples:

getMatchingPatterns('ttob'); // ['AABC']
getMatchingPatterns('ottb'); // ['ABBC']