string strPattern = @"^[\w\.-]{1,}@[a-zA-Z0-9][\w\.-]*\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";
System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(strPattern);
string email = "darrenTEST@gmail.com";
Response.Write(email + " - > " + regEx.IsMatch(email) + "<br/>");
email = "darren_東@gmail.com";
Response.Write(email + " - > " + regEx.IsMatch(email) + "<br/>");
// darrenTEST@gmail.com - > True
// darren_東@gmail.com - > True
把 \w 換成 A-Za-z0-9_ 就可以驗證過
string strPattern = @"^[a-zA-Z0-9_\.-]{1,}@[a-zA-Z0-9][\w\.-]*\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";
System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(strPattern);
string email = "darrenTEST@gmail.com";
Response.Write(email + " - > " + regEx.IsMatch(email) + "<br/>");
email = "darren_東@gmail.com";
Response.Write(email + " - > " + regEx.IsMatch(email) + "<br/>");
// darrenTEST@gmail.com - > True
// darren_東@gmail.com - > False
可是搬到 js 處理,就可以過
const regex = /^[\w\.-]{1,}@[a-zA-Z0-9][\w\.-]*\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
console.log(regex.test('darrenTEST@gmail.com')); // true
console.log(regex.test('darren_東@gmail.com')); // false
我想這是 C# 把 \w 當作 [word] 處理的關係,非符號都當作是文字
其他程式語言沒有測試過。不知是不是 c# 獨有的狀況