Javascript 在角度2+中使用全局替换方法;成分

Javascript 在角度2+中使用全局替换方法;成分,javascript,angular,typescript,Javascript,Angular,Typescript,我需要向现有组件添加一个名为“dataTestValue”的新属性。这是一个类似于选项卡的简单组件: export class TabComponent implements OnInit { @Input('active') active = false; @Input('title') title: string; dataTestValue: string; constructor() { } ngOnInit() { this.dataTestValu

我需要向现有组件添加一个名为“dataTestValue”的新属性。这是一个类似于选项卡的简单组件:

export class TabComponent implements OnInit {

  @Input('active') active = false;
  @Input('title') title: string;

  dataTestValue: string;

  constructor() { }

  ngOnInit() {
    this.dataTestValue = this.title.replace('/ /g', '_');
    console.log(this.dataTestValue);
  }
}

因为“title”属性可以包含空格,所以我需要替换它并将其转换为小写

我使用以下方法仅替换第一个空白:

this.dataTestValue = this.title.replace(' ', '_');
由于某种原因,全局方法不起作用,它只打印原始值。有什么想法吗


注:如果我正在使用“ngOnInit”生命周期挂钩,请通知我。

从正则表达式中删除单引号:

this.dataTestValue = this.title.replace(/ /g, '_');

要么删除引号

this.dataTestValue = this.title.replace(/ /g, '_');
或者你也可以用这个

this.dataTestValue = this.title.replace(/\s+/g, '_');

作品对不起,我的JavaScript技能很差