PrimeNG p-chips - 如果有特殊字符,如何删除?

PrimeNG p-chips - How to remove special characters if there are any?

如何强制用户使用 p-chips 组件只能输入字母、破折号和数字? p-chips 以外的其他组件不允许用于该项目,用正则表达式方法替换不起作用。

输入示例:abc-123 def(允许间距)

 onAdd(event) {

    //this line is to limit the total chips entered
    if (this.gridForm.value.inputTest.length >= 5) {
      this.gridForm.value.inputTest.splice(5);
    };

    //replace is not working although this line was hit on breakpoint
    this.gridForm.value.inputTest.forEach(m => {
       m = this.gridForm.value.inputTest.replace(/[^a-z0-9]/gi, '');
    })
  }

Prime-NG P-CHIPS Numbers only for Array of Numbers找到了类似的问题,但不确定如何修改

与另一个 post 中的方法相同,您可以检查用户输入是否与您的正则表达式匹配。如果没有,只需删除最后一个条目。这是实现此目的的一种方式:

checkInput(event) {
    if (
        event.value.replace(/[^a-z0-9\-\s]/gi, "").length < event.value.length
    ) {
        this.values.pop(); // remove last entry from values
    }
}

demo