练习
请尝试写一个验证Email地址的正则表达式。版本一应该可以验证出类似的Email:
'use strict';
var re = ^$;
// 测试:
var
i,
success = true,
should_pass = ['someone@gmail.com', 'bill.gates@microsoft.com', 'tom@voyager.org', 'bob2015@163.com'],
should_fail = ['test#gmail.com', 'bill@microsoft', 'bill%gates@ms.com', '@voyager.org'];
for (i = 0; i < should_pass.length; i++) {
if (!re.test(should_pass[i])) {
alert('测试失败: ' + should_pass[i]);
success = false;
break;
}
}
for (i = 0; i < should_fail.length; i++) {
if (re.test(should_fail[i])) {
alert('测试失败: ' + should_fail[i]);
success = false;
break;
}
}
if (success) {
alert('测试通过!');
}
版本二可以验证并提取出带名字的Email地址:
'use strict';
var re = ^$;
// 测试:
var r = re.exec('<Tom Paris> tom@voyager.org');
if (r === null || r.toString() !== ['<Tom Paris> tom@voyager.org', 'Tom Paris', 'tom@voyager.org'].toString()) {
alert('测试失败!');
}
else {
alert('测试成功!');
}