匹配完整的名字
function process_name() {
var field = document.getElementById("name_field");
var name = field.value;
var name_pattern = /^(S+) (S*) ?(S+)$/;
if (name_pattern.test(name) === false) {
alert("Name field is invalid");
return false;
}
var res = name_pattern.exec(name);
data.first_name = res[1];
data.last_name = res[3];
if (res[2].length > 0) {
data.middle_name = res[2];
}
return true;
}
^(S+)
: first name;
(S*) ?
: middle name;
(S+)$
: last name;
匹配邮件地址
var email_pattern = /^[^s@]+@[^s@.]+.[^s@]+$/;
^[^s@]+
: 开头值@
符号之间;
[^s@.]+
: @
至.
之间;
[^s@]+$
: .
至结尾;
匹配链接地址
var pattern = /^(?:https?://)?w+(?:.w+)?(?:.[A-Z]{2,3})+$/i
^(?:https?://)?w+
: http|https://xxxx
(?:.w+)?
: .xxxx
(?:.[A-Z]{2,3})+
: .xxxx