1
(Mr. | Mrs. | Dr. | Er.)으로 시작하는 문자열과 일치하는 문자열 [a- zA-Z]하지만 어쨌든 "Er.Dr."로 전달하는 중입니다.이니셜로 시작하는 문자열과 그 뒤에 유효한 알파벳 문자열이 일치합니다. - Regex
내가 정규식을 잘못 처리하고 있음을 알려주십시오.
var input = [
{str: "Mr.X", expectedValue: true},
{str: "Mrs.Y", expectedValue: true},
{str: "Dr#Joseph", expectedValue: false},
{str: "Er .Abc", expectedValue: false},
{str: "Er.Dr.", expectedValue: false},
{str: "Er.Abc", expectedValue: true}
];
var re = /^(Mr|Mrs|Ms|Dr|Er)\.+[A-Za-z]/;
input.forEach(x => {
var answer;
var matchStr;
answer = re.test(x.str);
matchStr = x.expectedValue === answer ? "MATCH" : "NO MATCH";
console.log("------------------------------------------------------------------------------");
console.log(`"${x.str}" | Expected Output: ${x.expectedValue} | My Output: ${answer} | ${matchStr}`);
console.log("------------------------------------------------------------------------------");
})
출력 -