var phone=\'13511115678\';
//方法一
var newphone=phone.substr(0, 3) + \'****\' + phone.substr(phone.length - 4);
//方法二
var newphone= phone.replace(phone.substr(3, 4),"****")
//方法三
var newphone=phone.replace(/(\d{3})\d{4}(\d{4})/, \'$1****$3\');
//方法四
var newphone=phone.replace( /([0-9]{3})([0-9]{4})([0-9]{4})/,"$1****$3")

详解方法三和四,如下:

此外,上述$1、$2、$3是将数字分隔开,比如讲中间四位和后四位互换位置  13511115678 -> 13556781111 

var newphone=phone.replace(/(\d{3})\d{4}(\d{4})/, \'$1$3$2\');  // 13556781111

版权声明:本文为renzm0318原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/renzm0318/p/10919543.html