Elio Farac
Elio Farac

Reputation: 11

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

For example:

asdq123""Prueba 2"

and I want

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;
  }

}

Upvotes: 1

Views: 979

Answers (2)

The fourth bird
The fourth bird

Reputation: 163467

You don't need to create a new variable, you can return the value of replace.

You can refactor the multiple replace calls to a single call using a pattern with an alternation and a character class.

ObservacionWeb|[:{}"]

If the word ObservacionWeb should not be part of a larger word, use word boundaries \bObservacionWeb\b

The update code using /g to replace all occurrences might look like:

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

Upvotes: 2

SeleM
SeleM

Reputation: 9658

You do not have to pipe it for one string (as mentioned in your question), Javascript's replaceAll do the job:

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

Check String.prototype.replaceAll().


Update:

If you want a pipe to replace many strings, update your pipe as following:

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

    return newValue1;
  }

This pipe would remove every ObservacionWeb, :, {, }, " found strings in a given string.
DEMO

Upvotes: 1

Related Questions