Angular app!

I need to hide the 2 second-last char of a string and replace them with "XX".

what i got:


what i need


How can i match this with a RegEx? I want to use this pattern in replace() method


That would look something like this:

let result = source.replace(/^(.*?).{2}(.{2})$/,'$1XX$2');


  • () is a "capture group"
  • ^ matches beginning of the string
  • .*? matches as few characters as necessary to fulfill pattern
  • .{2} matches two characters
  • $ matches the end of the string

And in the replacement string:

  • $# replaces the content of a capture group, numbered from 1 to #.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.