1
我想匹配一个以(先生|夫人|女士| Dr. |呃)跟随任何字符串[a- zA-Z],但它也通过了“Er.Dr.”。匹配字符串开头的首字母后跟一个有效的字母字符串 - 正则表达式
让我知道我在做什么在这里正则表达式的错误。
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("------------------------------------------------------------------------------");
})
输出 -