检测字符串的一组规则。
2.正则表达式的作用?
主要用于表单验证 处理复杂的字符串。
/正则表达式/标志位
2.构造函数方式:
new RegExp('正则表达式','标志位')
var re = /de/;
var str = 'how do you do';
console.log(str.search(re)); //-1
//1. 无g无组的情况
var re = /do/;
var str = 'how do you do';
console.log(re.exec(str)); //['do']
console.log(str.match(re)); //['do']
//2. 有g无组的情况
var re = /do/g;
var str = 'how do you do';
console.log(re.exec(str)); //['do']
console.log(str.match(re)); //['do','do']
//3. 无g有组的情况
var re = /([a-z]+) (\d+)/;
var str = 'haha 2022';
console.log(re.exec(str)); //['haha 2022','haha','2022']
console.log(str.match(re)); //['haha 2022','haha','2022']
//4. 有g有组的情况
var re = /([a-z]+) (\d+)/g;
var str = 'haha 2022,hehe 2023';
console.log(re.exec(str)); //['haha 2022','haha','2022']
console.log(str.match(re)); //['haha 2022', 'hehe 2023']
{m} : 表示{}前面的一个或一组字符连续出现 m 次
var str = 'ooo';
var re = /^o{3}$/;
console.log(re.test(str)); //true
{m,} : 表示{}前面的一个或一组字符连续出现 m 至 无限次,至少出现m次
var str = 'ooooooooooooooooooooo';
var re = /^o{3,}$/;
console.log(re.test(str)); //true
{m,n} : 表示{}前面的一个或一组字符连续出现 m 至 n次。
var str = 'oooo';
var re = /^o{3,5}$/;
console.log(re.test(str)); //true
2. [] : 表示取值范围
var re = /^[abc]{2,5}$/;
var str = 'aaaa';
console.log(re.test(str));
var re = /^[a-z]{2,5}$/;
var str = 'what';
console.log(re.test(str));
var re = /^[a-z]{2,5}$/i;
var str = 'What';
console.log(re.test(str));
var re = /^[a-zA-Z]{2,5}$/;
var str = 'What';
console.log(re.test(str));
var re = /^[0-9]{6}$/;
var str = '666888';
console.log(re.test(str));
var re = /^[a-zA-Z0-9_]{8,16}$/
var str = 'abc_123456';
console.log(re.test(str));
var re = /^[\u4e00-\u9fa5]{3,}$/
var str = '张小三';
console.log(re.test(str));
3. () : 表示组
var re = /^(中国){2,}$/
var str = '中国中国';
console.log(re.test(str));
var re = /^do*$/
var str = 'd';
console.log(re.test(str));
2. + : 表示+号前面的一个或一组字符连续出现 1 至 无限次 {1,}
var re = /^do+$/
var str = 'doooooooooooo';
console.log(re.test(str));
3. ? : 表示?号前面的一个或一组字符连续出现 0 至 1 次 {0,1}
var re = /^do?$/
var str = 'd';
console.log(re.test(str));
写在正则表达式的开头,表示断头,限制字符串的开头必须是指定的字符。 写在[]的开头,表示取反
var re = /^[^0-9]+/
var str = 'd45678';
console.log(re.test(str));
var re = /^[^0-9]+$/
var str = 'd!@#$%';
console.log(re.test(str));
2.$
表示断尾,限制字符串的结尾必须是指定的字符。
var re = /^h.*w$/;
var str = 'h2345r6t7y8uiokjfder56789iokw';
console.log(re.test(str));
var re = /^h.*w$/;
var str = 'h2345r6t7y8uiokjfder56789iokw';
console.log(re.test(str));
2. | : 表示或,一般结合组一起使用。
var re = /^(男|女)$/;
var str = '男';
console.log(re.test(str));
3. \ : 表示转义符,将具有特殊含义的符号转为普通字符。
//文件类型(后缀)(扩展名) .html .js .css .mp4 .c
var re = /\.[a-zA-Z0-9]{1,4}$/;
var str = 'index.html';
console.log(re.test(str));
\d : 表示数字 [0-9]
//邮政编码
var re = /^\d{6}$/;
var str = '034017';
console.log(re.test(str));
//手机号
var re = /^1(1|2|3|4|5|6|7|8|9)\d{9}$/;
var str = '17710875717';
console.log(re.test(str));
// 2022/7/20
var re = /^(\d{4}|\d{2})\/\d{1,2}\/\d{1,2}$/;
var str = '22/7/20';
console.log(re.test(str));
\D : 表示非数字 [^0-9] \w : 表示字母、数字、下划线 [a-zA-Z_]
//密码规则
var re = /^\w{8,16}$/;
var str = 'abc_12345678';
console.log(re.test(str));
\W : 表示 非(字母、数字、下划线) \s : 表示空白 \S : 表示非空白 \b : 表示单词边界 \B : 表示非边界
var pattern = /(google){4,8}/;
var str = 'googlegoogle';
alert(pattern.test(str));
var pattern = /(google|baidu|bing)/;
var str = 'google';
alert(pattern.test(str));
var pattern = /goo\sgle/;
var str = 'goo gle';
alert(pattern.test(str));
var pattern = /google\b/;
var str = 'google';
var str2= 'googleaa googlexx google dsdddd';
alert(pattern.test(str));
alert(pattern.test(str2));
var pattern = /g\w*gle/;
var str = 'google';
alert(pattern.test(str));
var pattern = /google\d*/;
var str = 'google444';
alert(pattern.test(str));
var pattern = /\D{7,}/;
var str = 'google8';
alert(pattern.test(str));
var pattern = /g[a-zA-Z_]*gle/;
var str = 'google';
alert(pattern.test(str));
var pattern = /g[^0-9]*gle/;
var str = 'google';
alert(pattern.test(str));
var pattern = /[a-z][A-Z]+/;
var str = 'gOOGLE';
alert(pattern.test(str));
var pattern = /g.*gle/;
var str = 'google';
alert(pattern.test(str));
var pattern = /^[a-z]+\s[0-9]{4}$/i;
var str = 'google 2012';
alert(pattern.exec(str));
var pattern = /^[a-z]+/i;
var str = 'google 2012';
alert(pattern.exec(str));
var pattern = /^([a-z]+)\s([0-9]{4})$/i;
var str = 'google 2012';
alert(pattern.exec(str)[0]);
alert(pattern.exec(str)[1]);
alert(pattern.exec(str)[2]);
var pattern = /Good/ig;
var str = “good good study!,day day up!”;
alert(str.replace(pattern,’hard’));
var pattern = /(.*)\s(.*)/;
var str = 'google baidu';
var result = str.replace(pattern, '$2 $1');
document.write(result)
var pattern = /8(.*)8/;
var str = 'This is 8google8';
var result = str.replace(pattern,'<strong>$1</strong>');
document.write(result);
Var pattern = /good/ig;
var str = ‘good good study!,day day up!’;
alert(str.match(pattern));
alert(str.match(pattern).length);
var pattern = /8(.*)8/;
var str = 'This is 8google8, dd 8ggg8';
alert(str.match(pattern));
alert(RegExp.$1);
var pattern = /good/ig;
var str = ‘good good study!,day day up!’;
alert(str.search(pattern));
/^(?![\d]+$)(?![a-zA-Z]+$)(?![\W]+$)[\da-zA-Z_!@#\.]{6,12}$/
阅读量:1013
点赞量:0
收藏量:0