I've been trying to use the .replace()
JavaScript function in Angular 4 to remove certain characters from a string. Here is the code snippet from my component:
@Component({...})
export class SomeComponent implements OnInit {
routerUrl: string = '';
constructor() {}
ngOnInit() {
this.routerUrl = "SOME_DYNAMICALLY_RETRIEVED_STRING";
this.routerUrl = this.routeUrl.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>{}/\[\]\\\/]/gi, '');
console.log(this.routerUrl);
}
However, I am encountering an error that says:
ERROR TypeError: Cannot read property 'replace' of undefined
at SafeSubscriber._next ...
I would appreciate any help in resolving this issue.
Additional Code
export class CourseComponent implements OnInit {
routeUrl: string = '';
constructor(private renderer: Renderer2, private route: ActivatedRoute) {}
ngOnInit() {
this.route.params.subscribe((params: Params) => {
this.routeUrl = params[':name'];
this.routeUrl = this.routeUrl.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>{}/\[\]\\\/]/gi, '');
console.log(this.routeUrl);
})
}
}