如何用空白 space 替换双引号? Angular

How do you replace double quotes with a blank space? Angular

例如:

asdq123""Prueba 2"

我想要

asdq123Prueba 2

replace.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'replace'
})
export class ReplacePipe implements PipeTransform {

  transform(value: string): unknown {
    let newValue1
    newValue1 = value.replace('ObservacionWeb','').replace(':','').replace('{','').replace('}','').replace('\"','');


    return newValue1;
  }

}

您不必将它用于一个字符串(如您的问题中所述),Javascript 的 replaceAll 可以完成这项工作:

const str = 'asdq123""Prueba 2"';
str.replaceAll('"', ''); // gives : asdq123Prueba 2

检查String.prototype.replaceAll()


更新:

如果您想要管道替换许多字符串,请按以下方式更新您的管道:

transform(value: string): unknown {
    let newValue1;
    newValue1 = value
      .replace(/ObservacionWeb/g, "")
      .replace(/\:/g, "")
      .replace(/\{/g, "")
      .replace(/\}/g, "")
      .replace(/\"/g, "");

    return newValue1;
  }

此管道将删除每个 ObservacionWeb:{}" 在给定字符串中找到的字符串。
DEMO

不需要新建变量,可以return取replace的值。

您可以使用具有 alternation and a character class.

的模式将多个替换调用重构为单个调用
ObservacionWeb|[:{}"]

如果 ObservacionWeb 一词不应该是更大词的一部分,请使用词边界 \bObservacionWeb\b

使用 /g 替换所有出现的更新代码可能如下所示:

return value.replace(/ObservacionWeb|[:{}"]/g, "");