Search code examples
javascriptregexlookbehindiban

regex to mask IBAN


I want to mask IBAN. I have to show only first 7 characters and last 4 characters.

Example:

Input:

PL61109010140000071219812874

Output:

PL61109***********2874

I wrote the following regex:

(?<=.{7})(.*)(?=.{4})

but js do not support lookbehind


Solution

  • You can use groups in Regex to do that instead of LookBehind

    var myString = "PL61109010140000071219812874";
    var myRegexp = /\w{7}(.*)\w{4}/g;
    var match = myRegexp.exec(myString);
    myString = myString.replace(match[1], '*********');
    console.log(myString)