Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

day13 #22

Closed
Wscats opened this issue Sep 4, 2017 · 2 comments
Closed

day13 #22

Wscats opened this issue Sep 4, 2017 · 2 comments

Comments

@Wscats
Copy link
Owner

Wscats commented Sep 4, 2017

//正则
//字面量
var box = /box/gi;
var str = "This is a Box boX"
//test
console.log(box.test(str));
console.log(/box/gi.test(str));
//exec
console.log(/box/gi.exec(str));
console.log(box.exec(str));

//string方法
//match
console.log(str.match(/box/gi));
//replace
console.log(str.replace(/box/gi, "xxx"));
//search
console.log(str.search(/box/gi));
//split
//console.log('a,b ,c , d, e'.split(', '));
console.log(str.split(/b/gi))

//常用
var pattern = /g..gle/; //一个点.匹配一个任意的字符
var str = "goagle";
console.log(str.match(pattern));

var pattern = /ge*gle/; //.* 匹配0到多个字符
var str = "gegle"
console.log(pattern.test(str));

var pattern = /g[^A-Z]*gle/;
var str = "gAgle";
console.log(pattern.test(str));

var pattern = /g[^a-z]*gle/; //可以有任意多个非0-9的字符
var str = "google";
console.log(pattern.test(str));

var pattern = /[a-z][A-Z]+le/;
var str = "aaGle";
console.log(pattern.test(str));

var pattern = /g\w*gle/;
var str = "gooA3_gle";
console.log(pattern.test(str));

var pattern = /g\d{2,4}gle/;
var str = "g3gle";
console.log(pattern.test(str));

var pattern = /goo\s+gle/;
var str = "goo  gle";
console.log(pattern.exec(str));

var pattern = /google|baidu|bing/; // | : 匹配三个中的其中一个字符串
var str = "googl2e bai3du;bingl"
console.log(pattern.exec(str));

var pattern = /google{4,8}/; //匹配分组中的字符出现4-8次
var str = "googleeeeeeeeee"
console.log(pattern.exec(str));

var pattern = /8(.*)8/;
var str = "this is 8google8 baab 8ggg8";
console.log(str.match(pattern));
console.log(pattern.exec(str))

var pattern = /[a-z]+$/i;
var str = "google 2016 gg";
console.log(str.match(pattern)); //"google"

var pattern = /^[a-z]+\s[0-9]{4}$/i;
var str = "google 2016";
console.log(pattern.exec(str)); //"google 2016"

var pattern = /^([a-z]+)\s([0-9]{4})$/i;
var str = "google 2016";
console.log(pattern.exec(str)); //"google 2016,google,2016"

//分组
var pattern = /(\d+)([A-Z])([a-z])/; //一个或多个数字,和一个a-z的字母
var str = "123Babc";
console.log(pattern.exec(str)); //"123a,123,a"

var pattern = /(\d+)([A-Z])(?:[a-z])/;
var str = "123Babc";
console.log(pattern.exec(str)); //"123a,123"

var pattern = /\.\[\/b\]/;
var str = ".[/bb]"
console.log(pattern.test(str));

var pattern = /\{\d+\}/;
var str = "{9}"
console.log(pattern.test(str));
@Wscats
Copy link
Owner Author

Wscats commented Sep 4, 2017

/*
			账号:
			* 不能为空,
			* 不能使用特殊字符(数字、字母、下划线、-),
			* 长度6-20
		 */
var _username = username.value;
if(!/^[\w\-]{6,20}$/.test(_username)) {
	alert('用户名不合法');
	return false;
}

/*
	昵称只能输入中文
 */
var _nickname = nickname.value;
if(!/^[\u2E80-\u9FFF]+$/.test(_nickname)) {
	alert('昵称必须为中文');
	return false;
}

/*
	电子邮件
	jinrong.xie@qq.com
	x1@qq.com
	x_2@163.com
	x-x@a-r.com.cn
 */
var _email = email.value;
if(!/^[\w\-\.]+@[\da-z\-]+(\.[a-z]{2,3}){1,2}$/.test(_email)) {
	alert('邮箱地址不合法');
	return false;
}

/*
	密码
	* 长度小于20
	* 不能包含空格
 */
var _psw = password.value;
if(!/^\S{1,19}$/.test(_psw)) {
	alert('密码不合法');
	return false;
}

/*
	身份证
	18/15位
	445655 19900707 2165
	445655 19900707 211x
 */
var _identity = identity.value;
if(!/^(\d{14}|\d{17})[\dx]$/.test(_identity)) {
	alert('身份证不合法');
	return false;
}

/*
	手机号码
 */
var _phone = phone.value;
if(!/^1[34578]\d{9}$/.test(_phone)) {
	alert('手机号不合法');
	return false;
}

/*
	生日 
	1999/05/08
	1999-5-8
	19990508
	1988/05/01
 */
var _birthday = birthday.value;
if(!/^\d{4}([\/\-\.]?)\d{1,2}\1\d{1,2}$/.test(_birthday)) {
	alert('生日格式不合法');
	return false;
}

@Wscats
Copy link
Owner Author

Wscats commented Sep 4, 2017

整数或者小数:^[0-9]+.{0,1}[0-9]{0,2}$
只能输入数字:"^[0-9]$"。
只能输入n位的数字:"^\d{n}$"。
只能输入至少n位的数字:"^\d{n,}$"。
只能输入m~n位的数字:。"^\d{m,n}$"
只能输入零和非零开头的数字:"^(0|[1-9][0-9]
)$"。
只能输入有两位小数的正实数:"^[0-9]+(.[0-9]{2})?$"。
只能输入有13位小数的正实数:"^[0-9]+(.[0-9]{1,3})?$"。
只能输入非零的正整数:"^+?[1-9][0-9]$"。
只能输入非零的负整数:"^-[1-9][]0-9"
$。
只能输入长度为3的字符:"^.{3}$"。
只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$"。
只能输入由26个大写英文字母组成的字符串:"^[A-Z]+$"。
只能输入由26个小写英文字母组成的字符串:"^[a-z]+$"。
只能输入由数字和26个英文字母组成的字符串:"^[A-Za-z0-9]+$"。
只能输入由数字、26个英文字母或者下划线组成的字符串:"^\w+$"。
验证用户密码:"^[a-zA-Z]\w{5,17}$"正确格式为:以字母开头,长度在6
18之间,只能包含字符、数字和下划线。
验证是否含有^%&',;=?$"等字符:"[^%&',;=?$\x22]+"。
只能输入汉字:"^[\u4e00-\u9fa5]{0,}$"
验证Email地址:"^\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)$"。
验证InternetURL:"^http://([\w-]+.)+[\w-]+(/[\w-./?%&=]
)?$"。
验证电话号码:"^((\d{3,4}-)|\d{3.4}-)?\d{7,8}$"正确格式为:"XXX-XXXXXXX"、"XXXX-XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"和"XXXXXXXX"。
验证身份证号(15位或18位数字):"^\d{15}|\d{18}$"。
验证一年的12个月:"^(0?[1-9]|1[0-2])$"正确格式为:"01"~"09"和"1"~"12"。
验证一个月的31天:"^((0?[1-9])|((1|2)[0-9])|30|31)$"正确格式为;"01"~"09"和"1"~"31"。 匹配中文字符的正则表达式: [\u4e00-\u9fa5]

@Wscats Wscats closed this as completed Sep 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant