【问题标题】:How do you replace double quotes with a blank space? Angular如何用空格替换双引号?角
【发布时间】:2021-01-05 13:05:40
【问题描述】:

例如:

asdq123""普鲁巴 2"

我想要

asdq123Prueba 2

替换.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;
  }

}

【问题讨论】:

  • 空白还是直接删除?
  • @selemmn 抱歉,删除它们

标签: angular replace pipe


【解决方案1】:

不需要新建变量,返回replace的值即可。

您可以使用alternationcharacter class 的模式将多个替换调用重构为单个调用。

ObservacionWeb|[:{}"]

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

使用/g 替换所有匹配项的更新代码可能如下所示:

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

【讨论】:

    【解决方案2】:

    您不必为一个字符串传输它(如您的问题中所述),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

    【讨论】:

    • 谢谢,但我显示的数据如下 {{ gestion.ObservacionWeb |替换 }} ,我想我不能应用你的方式,这就是我在管道中这样做的原因
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 2014-02-19
    • 1970-01-01
    • 2021-08-08
    • 2014-12-11
    • 1970-01-01
    相关资源
    最近更新 更多