How to use js to change the middle digits of the mobile phone number into ****

Today, we are going to implement a very common and simple function: changing the middle digits of the mobile phone number into ****

This function is actually very common, such as the mobile phone number displayed in our WeChat account security, the mobile phone number displayed in the Nuggets account settings, the ID number in Alipay, the App card number of major banks... there are many other things related to our privacy. I won’t list them one by one here. How can we implement seemingly simple functions?

In fact, there are many ways to achieve it. Here the author will use js to implement it. If there is anything incorrect or needs to be modified, please give me your advice. If you have a better method, you can also leave a message~

I believe that friends who have studied JavaScript are very familiar with the methods used below, but do you still remember their usage? Below the author will give a brief review of the methods used. Friends who don’t remember much should go and review it~

Okay, let’s stop talking nonsense and get to the point.

1. The first one is to use several common methods in strings and arrays. String method: split(). Array methods: splice(), join().

Before implementing it, let’s review these methods together:

  1. split(): Splits a string into an array of substrings and returns the array.

Note: This method returns the new array and does not change the original string. Note: If ("") is used as delimiter, the string will be split character by character. ​​www.w3school.com.cn/jsref/jsref…

Below is an example

const str = “hello”
let arr = str.split("");
console.log(arr); // h,e,l,l,o
  1. splice(): Method adds or removes items from the array and returns the removed item.

Note: ​splice()​This method will change the original array. ​​www.w3school.com.cn/jsref/jsref…

Below is an example:

const arr = ["a","b","c","d"];
arr.splice(1,2,"e","gg"); //从数字索引的第一位开始,删除两个索引的内容,向数组添加”e“he”gg“两项。
console.log(arr); // ["a","e","gg"];
  1. join(): Returns the array as a string. The elements will be separated by the specified delimiter. The default delimiter is comma (,).

Note: ​join()​This method will not change the original array

Below is an example:

const arr = ['a','b','b','d'];
let result = arr.join();
console.log(result); // a,b,c,d

Implementation function:

const telphone = '13300009999';
let telArr = telphone.split();
telArr.splice(3,4,'****');
let result = telArr.join(); //因为不会改变原数组,需要用一个新的变量去接收
console.log(result); // 1330****999
2. The second method is to use substr() of string
  1. substr(): This method is used to extract a part of the string. This method starts at the specified position and returns the specified number of characters .

Note: ​substr()​This method does not change the original string. Below is an example:

const str = 'Hello World';
let result = str.substr(1,4);
console.log(result); //ello

Implementation function:

const telphone = '13300009999';
let result = telphone.substr(0,4) + '****' + telphone.substr(8);
console.log(result);// 1330****999

//第二句代码解释:
//使用 `substr` 方法截取电话号码的前四位,然后拼接上'****',
//再拼接上电话号码从索引为8开始的剩余部分,将结果赋值给变量result。
3. The third method is to use substring() and replace() of strings;
  1. substring(): This method is used to extract the characters between the specified indexes (positions) from the string and return the substring. Note: ​substring()​This method does not change the original string. Below is an example:
const str = 'Hello World';
let result = str.substring(1,4);
console.log(result); //ell
  1. replace(): This method searches a string for a value or regular expression; this method returns a new string with the value replaced.

Note: ​replace()​This method does not change the original string. Below is an example:

const str = 'Hello World';
let result = str.replace("Hello","Hi");
console.log(result); //Hi World

Implementation function:

const telphone = '13300009999';
let result = telphone.replace(telphone.substring(3,8),'****');
console.log(result);// 1330****999

The above replace() mentioned regular expressions, then we can also use regular expressions to achieve www.w3school.com.cn/jsref/jsref… ​​​​​​​​www.w3school.com.cn/js/ js_regex

  1. /d: Find numbers
  2. [External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-1rhASd0H-1693210187039)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp /ae6ee234bd2d40b081d2b3b39d8ffc54~tplv-k3u1fbpfcp-image.image#?w=11&h=12&e=svg&a=1&b=000000)]2,…,$99: Text matching the 1st to 99th subexpressions in the regular rule.

Implementation function:

const telphone = '13300009999';
const reg = /(\d{4})\d{4}(\d{3})/;
let result = telphone.replace(reg,"$1****$2");
console.log(result);// 1330****999

To learn more about js, please pay attention to the CRMEB open source mall , which provides free download and learning.

Guess you like

Origin blog.csdn.net/CRMEB/article/details/132541285