projects/app-base-library/src/lib/angular/pipes/replace-all.pipe.ts
name | replaceAll |
transform | ||||||||||||||||||||
transform(value: string, stringToReplace: string, replacement: string, ignoreCase: boolean)
|
||||||||||||||||||||
Parameters :
Returns :
any
|
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'replaceAll'
})
export class ReplaceAllPipe implements PipeTransform {
transform(value: string, stringToReplace: string, replacement: string, ignoreCase: boolean = false ): any {
if (!value) {
return value;
}
function replaceAll(src, str1, str2, ignore) {
return String(src).replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g, '\\$&'), (ignore ? 'gi' : 'g')), (typeof(str2) === 'string') ? str2.replace(/\$/g, '$$$$') : str2);
}
return replaceAll(value, stringToReplace, replacement, ignoreCase);
}
}