假设我得到一个带有特殊字符的字符串,并且我想使用过滤器/管道来更改它。此外,每个单词的第一个字母应该大写。
例如,"@!₪ test stri&!ng₪"
将变为 "Test String"
。
如何才能做到这一点?
最佳答案
您可以按如下方式使用管道,
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'specialPipe'
})
export class specialPipe implements PipeTransform {
transform(value: string): string {
let newVal = value.replace(/[^\w\s]/gi, '')
return newVal.charAt(1).toUpperCase() + newVal.slice(2);
}
}
<强> DEMO STACKBLITZ
关于javascript - 从 Angular 字符串中删除特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52676879/