var str = "上海,上天,上哪"
var reg = /上[海天哪]/g
console.log(str.replace(reg,"*")); //**,**,**
var s = "你sb,它sx,他sd"
var r = /[A-z]/g
console.log(s.replace(r,"*")); //你**,它**,他**
var str = "it4545e12m3456"
// 4545123456
var reg = /[A-z]/g
console.log(str.replace(reg,""));
去掉字母
var str = "http://www.baidu.com/sunject/1233/WQW"
var reg = /[A-z0-9]/g
console.log(str.replace(reg,"*"));//****://***.*****.***/*******/****/***
var str = "https://www.baidu.com/123456"
// https://www.baidu.com/
var reg = /[0-9]/g
console.log(str.replace(reg,""));//https://www.baidu.com/
var arr = [
{id:1001,name:"你好3455ffd"},
{id:1001,name:"他家理发店34adasfafd"},
{id:1001,name:"附加费3455asdfd"},
]
/*
[
{id:1001,name:'你好...'}
]
*/
var r = /[A-z0-9]/g
var res = arr.map(item=>{
var test = item.name.replace(r,"")+"..."
var list = {
[item.id]:test
}
return list
})
console.log(res);
将原数组处理成[{id:1001,name:'你好...'}]
var phone = "02788667777"
/*
027?
8
\d{7}
*/
// var reg = /^...$/ 严格匹配 整个字符串必须满足正则
var reg = /^(027)?8\d{7}$/
console.log(reg.test(phone));
手机号实例
在输入框输入电话号码,如果格式不正确就弹出输入有误
<input type="text" id="app">
<p style="display: none;">输入有误</p>
<script>
/*
(086)?1[3456789]\d{9}
*/
var reg = /^(086)?1[^0-2]\d{9}$/
$('#app').keyup(function(event){
if(event.keyCode === 13){
var value = $(this).val()
console.log(reg.test(value));
if(!reg.test(value)){
$("p").css("display","block")
}
}
})
<input type="text" id="app" placeholder="请输入密码">
<script>
/*
420983200007136010
42\d{15}[\dx]
42\d{15}(\dx)
*/
var str = '420983200007136010'
var reg = /^42\d{15}[\dx]$/
console.log(reg.test(str));
/*
密码
包含大写小写至少6位
*/
// var r = /^[A-z]+\d{6,}$/
var r = /^[A-z+\d]{6,}$/
$('#app').keyup(function(event){
if(event.keyCode === 13){
var value = $(this).val()
console.log(r.test(value));
}
})
</script>
身份证号实例
var str = "<span>2395人参观</span>-<span>46人评价</span>"
// var arr = [2395,45]
var reg = /[^\d-]/g
var arr = []
arr.push(str.replace(reg,"").split("-"))
console.log(arr);//var arr = [2395,45]
筛选旧数组获取新的数组
<p class="app">
导演: 陈凯歌 Kaige Chen 主演: 张国荣 Leslie Cheung / 张丰毅 Fengyi Zha...<br>
1993 / 中国大陆 中国香港 / 剧情 爱情 同性
</p>
<script>
var value = $('.app').html().trim().split("<br>")[1].trim()
// [1993,中国大陆,剧情,爱情,同性]
var reg = / |中国香港/g
console.log(value.replace(reg,""));
var res = value.replace(reg,"").split("/")
var list = res[res.length-1].split(" ")
var sum = []
for(var i=0;i<res.length;i++){
if(i<res.length-1){
sum.push(res[i])
}
}
sum.push(...list)
console.log(sum);
</script>
获取一个这样的[1993,中国大陆,剧情,爱情,同性]数组
<input type="text" id="app" placeholder="请输入你的邮箱">
<script>
/*
邮箱验证
2820163916@qq.com
cheng_dong@sina.com
*/
var reg = /^\w+@[a-z]+\.com$/
$('#app').keyup(function(event){
if(event.keyCode === 13){
var value = $(this).val()
console.log(reg.test(value));
}
})
</script>
邮箱验证
var str = "<p>冯大大发</p><br><span>666</span>"
var reg = /<[^<>]+>/g
console.log(str.replace(reg,""));
获取标签内的文本
var url = "https://movie.douban.com/subject/129205/"
// [[1,2],[9,2],[0,5]]
var reg = /[^\d]/g
var res = url.replace(reg,"")
var arr = res.split("")
var test = []
for(var i=0;i<arr.length;i+=2){
test.push(arr.slice(i,i+2))
}
console.log(test);
获取url字符串里的数字,并将其拆分成二维数组