javascriptnode.jsregex

Hide some digit of user mobile no for security reason


if u what to show any user Mobile on your website but as per security reason you are not display full Mobile No like 9305070207 you only show 93*****207. try this regex -:

 var str="9305070207";
        var str1="9305070207";
        var formetNum = str.replace(/\d{8}$/,'')+str1.replace(/^\d{7}/,'*****');
        document.write(formetNum);

if any better way of this process please tell me . i improve my knowledge


Solution

  • You could get the first two numbers and replace all numbers until three character are left over.

    It works for any length of a string.

    var string = '9305070207',
        replaced= string.slice(0, 2) + string.slice(2).replace(/.(?=...)/g, '*');
    
    console.log(replaced);