<script>
/*
规定字符出行的次数
正则 test()
*/
var reg = /[0-9]{6}/
var pwd = 1234576;
var t = reg.test(pwd);
console.log(t)
</script>
<!--
{m} 就出行m次
{m,}至少出行m次
{m,n}
-->
<script>
var str = "2123jhfdd"
var reg = /\d{6,}/;
console.log(reg.test(str))
</script>
<!--
{3,}
{3}
{3,6}
?{0,1}
+{1,}
*{0,}
-->
<script>
var str = "hello123";
var reg = /[a-z]+/;
console.log(str.replace(reg,"*"))
console.log(str.replace(/[a-z]*/,"*"))
</script>
贪婪和懒惰
<script>
// 贪婪模式:在给定量词的情况下,会取最大的量词
var str = "1231231hello";
var reg = /\d{3,6}/;
console.log(str.replace(reg,"*"))
//test 只要正则表达式的某一段满足,就输出结果为true
console.log(reg.test(str))
// tips:+,*默认就是贪婪模式
</script>
懒惰模式
<script>
//懒惰模式
var str = "123123hello";
var reg = /\d{3,6}?/;
console.log(str.replace(reg,"*"))
</script>
选择和分组
<script>
/*
|
();
*/
var str = "你去哪里,他在..."
var reg = /你去|他在/g;
console.log(str.replace(reg,"*"))
</script>
<script>
var url = "http://www.baidu.com";
var https = "https:www.daidu.com"
var reg = /(http|https):\/\/www\baidu\.com/;
console.log(reg.test(url))
console.log(reg.test(https))
</script>
<script>
var phone = "02788667777";
//^ 后面一个$ 严格匹配的意思必须满足正则条件
var reg = /^(027)?8\d{7}$/;
console.log(reg.test(phone))
</script>
<input type="text" id="app">
<script>
//(086)?1[356789]\d{9}$
var reg = /^(086)?1[3-9]\d{9}$/;
$("#app").keyup(function(event){
if(event.keyCode ==13){
var value = $(this).val();
console.log(reg.test(value))
}
})
</script>