用正则替换手机号码
<!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>