Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rowdyrabouw/479ca434db0c3bf59e663f61314b9213 to your computer and use it in GitHub Desktop.
Save rowdyrabouw/479ca434db0c3bf59e663f61314b9213 to your computer and use it in GitHub Desktop.
Custom Angular pipe for formatting IBAN
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'iban' })
export class Iban implements PipeTransform {
transform(value: string): string {
// remove existing spaces
let lIban: string = value.replace(" ", "");
// place a space after every 4th character
lIban = lIban.replace(/(.{4})/g, "$1 ");
console.log(lIban);
return lIban;
}
}
@pedambr
Copy link

pedambr commented Feb 20, 2018

nice pipe.
keep in mind that string.replace on line 6 only replaces the first space

@Adrian1907
Copy link

Slightly shorter version:
...
return value.split(' ').join('').replace(/(.{4})/g, "$1 ");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment