1、过滤数字字母以…结尾
<script>
var str = "https://www.baidu.com/12455";
var reg = /[0-9]/g;
console.log(str.replace(reg,""));
var arr = [
{id:1001,name:"你好3455ffd"},
{id:1001,name:"互戳撒网我3455ffd"},
{id:1001,name:"后台服务3455ffd"}
]
var reg = /[0-9a-z]/g;
arr.forEach(item=>{
var name = item.name.replace(reg,"")+"...";
item.name = name;
})
console.log(arr);
</script>
2、输入手机号
<body>
<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>
</body>
3、var reg = /(http|https):\/\/www.baidu.com/;
<script>
var url = "http://www.baidu.com";
var https = "http://www.baidu.com";
var reg = /(http|https):\/\/www\.baidu\.com/;
console.log(reg.test(url))
console.log(reg.test(https))
</script>
4、身份证
<script>
//42...x
// 42\d{15} (\d|x)
// 42\d{15}[\dx]
var str = "420528199108282529"
var reg = /^42\d{15}[\dx]$/;
console.log(reg.test(str));
</script>
5、密码
<input type="text" id="app">
<script>
//密码 必须包含大写小写至少六位数字
var reg = /^[A-Z]+[a-z]\d{6,}$/ //开头必须是大写字母
$("#app").keyup(function (event) {
if (event.keyCode == 13) {
var value = $(this).val();
console.log(value);
console.log(reg.test(value));
}
})
</script>
6、豆瓣 霸王别姬
<p class="">
导演: 陈凯歌 Kaige Chen 主演: 张国荣 Leslie Cheung / 张丰毅 Fengyi Zha...<br>
1993 / 中国大陆 / 剧情 爱情 同性
</p>
<script>
//[1993,中国大陆,剧情 爱情 同性]
var str = $("p").html().trim().split("<br>")[1].trim();
var reg = / /g
var res = str.replace(reg,"");
var arr = res.split("/");
var last = arr[arr.length-1].split(" ");
console.log(last);
// var sum = [];
// for(var i = 0; i<arr.length; i++){
// if(i<arr.length-1){
// sum.push(arr[i]);
// }
// }
// sum.push(...last);
var sum = [arr[0],arr[1],...last];
console.log(sum);
</script>
7、邮箱验证
<input type="text" placeholder="请输入你的邮箱">
<script>
// 邮箱验证 39775347@qq.com
// cheng_benchao@sina.com
var reg = /^\w+@[a-z]+\.com$/
$("input").keydown(function(event){
if(event.keyCode == 13){
var value = $(this).val();
console.log(value)
console.log(reg.test(value));
}
})
</script>
<script>
var url = "https://movie.douban.com/subject/129154/";
var reg = /[^\d]/g
var str = url.replace(reg,"");
var sum = [];
for(var i = 0; i<str.length; i+=2){
var item = str.substr(i,2).split("");
sum.push(item);
}
console.log(sum);
</script>