1. JavaScript replaces the 4 digits in the middle of the mobile phone number
// Match the beginning and end of the mobile phone number, output '12345678901'.replace(/(/d{3})/d{4}(/d{4})/, '$1****$2');Example
<!doctype html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>Unt titled document</title><script type="text/javascript">var phone='12345678901';var dh=phone.replace(/(/d{3})/d{4}(/d{4})/, '$1****$2');alert (dh);</script></head><body></body></html>Note: This section regularly matches the 11 consecutive digits in the string, replaces the 4 middle digits as *, and outputs the common format of hidden mobile phone numbers. If you want to get only the last 4 digits, you can change it to the following form:
2. JavaScript replaces the first 7 digits of mobile phone number
// Match 11 consecutive digits and replace the first 7 digits with * number '15110280327'.replace(//d{7}(/d{4})/, '******$1');Example
<!doctype html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>Unt titled document</title><script type="text/javascript">var phone='12345678901';var dh=phone.replace(//d{7}(/d{4})/, '******$1');alert (dh);</script></head><body></body></html>Supplementary Note: Parentheses in regular expressions can be used for grouping, and they are also used to define sub-mode strings. In the replace() method, $n (n is a number) can be used in parameter 2 to refer to the strings defined in the pattern string in sequence.
3. JavaScript mobile phone verification and four-digit comprehensive examples of hidden mobile phone numbers
<!doctype html><html lang="en"><head> <meta charset="UTF-8" /> <title>js mobile phone number verification and hidden middle four digits</title> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script></head><body> <input type="text" id="myText"> <p>js mobile phone verification and hidden middle four digits</p> <input type="button" value="submit" id="subBtn"> <script type='text/javascript'> $(function(){ $("#subBtn").click(function(){ if($("#myText").val()==""){ alert("Mobile phone number cannot be empty") }else{ if(iphoneCheck(myText)){ alert("Subsubscription successfully"); var phone=$("#myText").val(); var myphone=phone.substr(3,4); //alert(myphone) var lphone=phone.replace(myphone,"****"); $("#myText").val(lphone); }else{ alert("Please enter the correct mobile phone number") } } function iphoneCheck(id){ var temp=document.getElementById("myText"); var re=/^[1][34587]/d{9}$/;//Mobile phone number verification regular expression if(re.test(temp.value)){ return true; }else{ return false; } } }); }); </script></body></html>Summarize
The above is all about javascript verifying mobile phone numbers and implementing asterisk (*) replacement effect. I hope that the content of this article will be helpful to everyone's daily use of JavaScript.