skip to Main Content
let regex = /(^d{6})|(d{3}$)/gm;
n = n.toString();
n = n.replace(regex,'#');
return '#####' + n + "##"

This is my regex but it returns #5225# not the expected result which is ######5225###.

2

Answers


  1. just you string slice, remove the first 6 and last 3 and return the result with additional # before and after

    function replaceChar( char ) {
        return '######' + char.toString().slice(6,-3) + '###'
    }
    console.log(replaceChar(9999991111333))
    Login or Signup to reply.
  2. Use a replacer function and use repeat with the length of the match.

    const regex = /(^d{6})|(d{3}$)/gm;
    let n = 111111234555;
    n = n.toString();
    n = n.replace(regex, x => '#'.repeat(x.length));
    console.log(n);

    or using substring

    let n = 111111234555;
    const result = '######' + n.toString().substring(6,9) + '###';
    console.log(result);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search