import { Injectable } from '@angular/core' ; @ Injectable ( ) export class ValidatorService { private static readonly regex = new RegExp ( '[.*+\\-?^${}()|[\\]\\\\]' ) ; public static getRegex ( ) { return this . regex ; } public static replaceTextWithRegex ( value ) { return value . replace ( /[.*+\-?^${}()|[\]\\]/g , '\\$&' ) ; } public static validateRegex ( input ) { let regexObj ; try { regexObj = new RegExp ( input ) ; } catch ( e ) { regexObj = new RegExp ( input . replace ( /[.*+\-?^${}()|[\]\\]/g , '\\$&' ) ) ; } if ( regexObj . source . includes ( '#' ) ) { regexObj = new RegExp ( regexObj . source . replace ( '#' , '\\#' ) ) ; } if ( regexObj . source . includes ( '[]' ) ) { regexObj = new RegExp ( regexObj . source . replace ( '[]' , '\\[\\]' ) ) ; } return regexObj . source ; } }