-1

I have the following string, where I would like to remove all instances of EUR

var str = "200.00 EUR, 33.33 EUR, 100.95 EUR, 300 EUR";

So the output is:

var str = "200.00, 33.33, 100.95, 300";

I tried

var res = str.replace("EUR", "");

But that only removes EUR one time.

apples-oranges
  • 959
  • 2
  • 9
  • 21

3 Answers3

6

You can use regular expression to replace the string. Keep in mind string are immutable so original string will be unchanged.

var str = "200.00 EUR, 33.33 EUR, 100.95 EUR, 300 EUR";
var str1 = str.replace( /\s*EUR/g, "");
console.log(str1);

In string replace() it will search for the word 'EUR' and while it found the required value it will replace with the given value. As it found the required value and replace it. it wont search any further. That's why you need regular expression with /g flag. without /g flag it will work same

atiq1589
  • 2,227
  • 1
  • 16
  • 24
5

Try like this with a global replacement: by /g flag on replace()

Where, g is global case-sensitive replacement

var string  = '200.00 EUR, 33.33 EUR, 100.95 EUR, 300 EUR';
string = string.replace(/ EUR/g, '');
console.log(string);

Global replace can only be done with a regular expression. In the following example, the regular expression includes the global which permits replace() to replace each occurrence of ' EUR' in the string with ''

See Demo : https://jsfiddle.net/t7jz3h9k/11/

As per comment:

var string  = '200.00 EUR, 33.33 PND, 100.95 DLR, 300 EUR';
string = string.replace(/ EUR| PND| DLR/g, '');
console.log(string);
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
2

Easy, str.replace(/EUR/g, '');

Drewness
  • 5,004
  • 4
  • 32
  • 50