Home > Mobile >  Regex pattern doesn't work for 2 characters?
Regex pattern doesn't work for 2 characters?

Time:11-07

I have been trying to create a regex pattern to validate names which would support alphabets, acute accent characters and including these 2 special characters - .

This is the pattern and it works as expected but only after I enter a 3rd character!

So, it doesn't work for 2 characters and returns false.

/^[a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\.\-]([a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\s\.\-] )([a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\s\.\-]$)/.test('RE')

It works perfectly for >=3 characters and returns true.

/^[a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\.\-]([a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\s\.\-] )([a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\s\.\-]$)/.test('REG')

What could be going wrong with the pattern which doesn't work for chars less than 3?

Does somebody here recognize this?

CodePudding user response:

The pattern explicitly requires three or more characters:

  1. One to match [a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\.\-]

  2. One or more to match ([a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\s\.\-] )

  3. One to match ([a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\s\.\-])

You haven't said what you want the pattern to do and why you're capturing the middle and final bit separately, but if you want to capture the "middle" and the end separately, change the quantifier to * instead; a two-character string will capture the empty string for the first capturing group:

/^[a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\.\-]([a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\s\.\-]*)([a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\s\.\-])$/
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^

const rex = /^[a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\.\-]([a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\s\.\-]*)([a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\s\.\-])$/;

function test(str) {
    const match = rex.exec(str);
    console.log(`String:  ${str}`)
    console.log(`Result:  ${match ? "match" : "no match"}`);
    if (match ) {
      console.log(`Group 1: ${match[1]}`);
      console.log(`Group 2: ${match[2]}`);
    }
}

test("RE");
test("REG");
.as-console-wrapper {
    max-height: 100% !important;
}

Or perhaps you meant to capture everything after the first char (since the second and third classes are the same):

/^[a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\.\-]([a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\s\.\-] )$/

const rex = /^[a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\.\-]([a-zA-ZßẞüÜöÖäÄÑñÁáÀàÂâÉéÈèÊêËëÍíÌìÎîÏïIJijÓóÒòÔôÚúÙùÛûÝýŸÿ\s\.\-] )$/;

function test(str) {
    const match = rex.exec(str);
    console.log(`String:  ${str}`)
    console.log(`Result:  ${match ? "match" : "no match"}`);
    if (match ) {
      console.log(`Group 1: ${match[1]}`);
    }
}

test("RE");
test("REG");
.as-console-wrapper {
    max-height: 100% !important;
}

CodePudding user response:

But your pattern is matching 3 chars min? If you rewrite it:

/^[<char>(<char> )<char>$/

It is three chars min

  • Related