前端切图:用正则替换手机号码
用正则替换手机号码
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<title></title>
</head>
<body>
</body>
<script>
<!--用三个*号替换中间的数字-->
var phone="18895731936";
varreg2=/([0-9]{4})([0-9]{3})([0-9]{4})/;
console.log(phone.replace(reg2,"$1***$3"));
</script>
<!--用属性值替换属性-->
<script>
+function(){
varobj={
"name":"王婷",
"age":"22",
"sex":"男",
}
varstr="0000name00234age121312sex1231";
varstr1=str.replace(/([a-z]+)/,function(value,$1){
//$1是第一个括号的值
returnobj[$1]||value;
});
console.log(str1);
}();
</script>
</html>
![5640239-703e586cbdb0a978.png](https://upload-images.jianshu.io/upload_images/5640239-703e586cbdb0a978.png)
图片发自简书App
更多技术交流,前端微信群,联系我微信,拉你进群