1. JavaScript menggantikan 4 digit di tengah nomor ponsel
// Cocokkan awal dan akhir dari nomor ponsel, output '12345678901'.replace (/(/d {3})/d {4} (/d {4})/,' $ 1 **** $ 2 ');Contoh
<!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 Telepon = '12345678901'; var dh = phone.replace (/(/d {3})/d {4} (/d {4})/, '$ 1 **** $ 2'); waspada (dh); </script> </head> <body> </body> </html>Catatan: Bagian ini secara teratur cocok dengan 11 digit berturut -turut dalam string, menggantikan 4 digit tengah sebagai *, dan mengeluarkan format umum dari nomor ponsel tersembunyi. Jika Anda hanya ingin mendapatkan 4 digit terakhir, Anda dapat mengubahnya ke formulir berikut:
2. JavaScript menggantikan 7 digit pertama nomor ponsel
// Cocokkan 11 digit berturut -turut dan ganti 7 digit pertama dengan*nomor '15110280327'.replace (// d {7} (/d {4})/,' ****** $ 1 ');Contoh
<!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 Telepon = '12345678901'; var dh = phone.replace (// d {7} (/d {4})/, '****** $ 1'); waspada (dh); </script> </head> <body> </body> </html>CATATAN TAMBAHAN: tanda kurung dalam ekspresi reguler dapat digunakan untuk pengelompokan, dan mereka juga digunakan untuk mendefinisikan string sub-mode. Dalam metode ganti (), $ n (n adalah angka) dapat digunakan dalam parameter 2 untuk merujuk pada string yang ditentukan dalam string pola secara berurutan.
3. Verifikasi ponsel JavaScript dan contoh komprehensif empat digit nomor ponsel tersembunyi
<! Doctype html> <html lang = "en"> <head> <meta charset = "utf-8"/> <itement> verifikasi nomor ponsel JS dan tersembunyi empat digit </itement> <script type = "Text/JavaScript" src = "http://code.jqery.com/jqery <injquy" script/script/http://code.jqery.com/jQuery.com type = "text" id = "myText"> <p> JS verifikasi ponsel dan empat digit tengah tersembunyi </p> <input type = "Tombol" value = "kirim" id = "Subbtn"> <script type = 'Text/JavaScript'> $ (function () {$ ("#Subbtn"). Klik () {{) {() () {"). Nomor telepon tidak dapat kosong ")} else {if (iPhoneCheck (myText)) {alert (" Subsubscription berhasil "); var phone = $ ("#mytext "). val (); var myphone = phone.substr (3,4); // alert (myphone) var lphone = telepon. } {alert ("Harap masukkan nomor ponsel yang benar")}} Fungsi iPhoneCheck (id) {var temp = document.getElementById ("mytext"); }); </script> </body> </html>Meringkaskan
Di atas adalah semua tentang JavaScript memverifikasi nomor ponsel dan mengimplementasikan efek penggantian asterisk (*). Saya berharap konten artikel ini akan bermanfaat untuk penggunaan JavaScript setiap hari setiap hari.