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("------------------------------------------------------------------------------");
})
輸出 -