备选字符集
var str ="上海,上天,上哪里去";
var reg =/上[海天]/g
console.log(str.replace(reg,"*")); // *,*,上哪里去
var str = "上海,上天,上哪里"
var reg = /[上天哪]/g
console.log(str.replace(reg,"*")); // *海,**,**里
var res = "ajkdgkbcj128973489"
var reg = /[0-9]/g
console.log(res.replace(reg,"*")); // ajkdgkbcj*********
var str = "hello"
var reg = /[el]o/g
console.log(str.replace(reg,"*")); // hel*
转义字符
var str = "https://www.baidu.com"
var str2 = "http://www.baidu.com"
var reg = /(http|https):\/\/[w]{3}\.baidu\.com/
console.log(reg.test(str));
console.log(reg.test(str2));
指定匹配的位置
// 电话
var num = "13897932473123"
var reg = /^1[3-9]\d{9}$/
console.log(reg.test(num));
<input type="text" id="input">
<script>
/* 去除开头结尾的空格 */
var res = []
var reg = /^\s+|\s+$/g;
var input = document.getElementById("input")
input.onkeydown = function(event){
if(event.keyCode == 13){
var str = this.value
res.push(str.replace(reg,""))
console.log(res);
}
}
</script>
非
var str = "abc123";
var reg = /[^abc]/g;
console.log(str.replace(reg,"*")); //abc***
var test = "?@hello123-";
var re = /[^\d]/g;
console.log(test.replace(re,"*")); //*******123*
var str = "hello210874"
var reg = /[^a-z]/g
console.log(str.replace(reg,"")); // hello
var str = "<p>我是p标签</p>。<div>我是div</div>"
var reg = /<[^<>]+>/g
console.log(str.replace(reg,"")); // 我是p标签。我是div
var str = "https://movie.douban.com/subject/129205/"
var reg = /[^\d+]/g
console.log(str.replace(reg,"")); // 129205
表单验证
手机号验证
<input type="text" id="app">
<script>
var reg = /^(086)?1[3-9]\d{9}$/ ;
$("#app").keydown(function(event){
if(event.keyCode == 13){
var value = $(this).val();
console.log(reg.test(value))
}
})
</script>
身份证号验证
<script>
/*
42......x
42900619881024307x
42\d{15}(\d|x)
42\d{15}[\dx]
*/
var str = "42900619881024307x";
var reg = /^42\d{15}[\dx]$/;
console.log(reg.test(str))
</script>
邮箱验证
39775347@qq.com
cheng_chao@sina.com
<script>
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>
指定匹配位置
var str = "fdjfj121"
var reg = /^[0-9]+/;
console.log(reg.test(str)) //false
console.log(/\d+$/.test(str)) //true
var str = " hello ";
var reg = /(^\s+)|(\s+$)/g
console.log(str.trim()) //hello
console.log(str.replace(reg,"")) //hello
获取数字变成数组
<script>
var str = "<span>2395人参观</span>-<span>46人评价</span>"
/*
var arr = [2395,46]
*/
var reg = /[^\d-]/g;
var res = str.replace(reg,"");
console.log(res.split("-"))
</script>
处理文本内容
<p class="">
导演: 陈凯歌 Kaige Chen 主演: 张国荣 Leslie Cheung / 张丰毅 Fengyi Zha...<br>
1993 / 中国大陆 / 剧情 爱情 同性
</p>
<script>
/* [1993,中国大陆,剧情,爱情,同性] */
/* 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 = [arr[0],arr[1],...last];
// for( var i=0;i<arr.length;i++){
// if(i<arr.length-1){
// sum.push(arr[i])
// }
// }
// sum.push(...last)
console.log(sum)
</script>
<script>
var url = "https://movie.douban.com/subject/129205/";
/* [[1,2],[9,2],[0,5]] */
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>