Created
May 26, 2017 17:59
-
-
Save rowdyrabouw/479ca434db0c3bf59e663f61314b9213 to your computer and use it in GitHub Desktop.
Custom Angular pipe for formatting IBAN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
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
nice pipe.
keep in mind that string.replace on line 6 only replaces the first space