Use javascript to verify the mobile phone number and implement asterisks (*) to replace the instance, and use javascript asterisks
I. use JavaScript to replace four digits in the middle of the mobile phone number
// Match the beginning and end of the mobile phone number, and output '123' in a format similar to "123 *** 8901 '. replace (/(\ d {3}) \ d {4} (\ d {4})/, '$1 ***** $2 ');
Example
<! Doctype html>
Note:This regular expression matches 11 consecutive numbers in the string, replacing the four digits in the middle with the "*" character, and output the common format of the hidden mobile phone number. If you want to get only the last four digits, you can change it to the following format:
2. Use JavaScript to replace the first seven digits of the mobile phone number
// Match 11 consecutive digits, and replace the first seven digits with the number * '123 '. replace (/\ d {7} (\ d {4})/, '******** $1 ');
Example
<! Doctype html>
Additional Notes:Parentheses in the regular expression can be used for grouping and defining sub-pattern strings. In the replace () method, $ n (n is a number) can be used in parameter 2) to reference strings defined in parentheses in a pattern string in sequence.
Iii. JavaScriptMobile phone verification and hidden four-digit comprehensive example of mobile phone number
<! Doctype html>
Summary
The above is all the content of the javascript verification mobile phone number and the implementation of asterisk (*) to replace the effect, I hope the content of this article will be helpful for your daily use of JavaScript.