0

I'm trying to remove the euro sign from my string.

Since the string looks like this €33.0000 - €37.5000, I first explode to string on the - after I try to remove the euro sign.

var string = jQuery('#amount').val();
var arr = string.split(' - ');

if(arr[0] == arr[1]){
    jQuery(this).find('.last').css("display", "none");
}else{
    for(var i=0; i< arr.length; i++){
        arr[i].replace('€','');
        console.log(arr[i]);
    }
}

When I try it on my site, the euro signs aren't removed, when I get the string like this

var string = jQuery('#amount').val().replace("€", "");

Only the first euro sign is removed

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3164891
  • 298
  • 2
  • 4
  • 14

5 Answers5

1

.replace() replace only the fisrt occurence with a string, and replace all occurences with a RegExp:

jQuery('#amount').val().replace(/€/g, "")
KyleK
  • 4,643
  • 17
  • 33
0

Try using a regular expression with global replace flag:

"€33.0000 - €37.5000".replace(/€/g,"")
robobot3000
  • 589
  • 2
  • 4
0

First get rid of the € (Globally), than split the string into Array parts

var noeur = str.replace(/€/g, '');
var parts = noeur.split(" - ");
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
0

The problem with your first attempt is that the replace() method returns a new string. It does not alter the one it executes on.

So it should be arr[i] = arr[i].replace('€','');

Also the replace method, by default, replaces the 1st occurrence only.

You can use the regular expression support and pass the global modifier g so that it applies to the whole string

var string = Query('#amount').val().replace(/€/g, "");
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
0
var parts = /^€([0-9.]+) - €([0-9.]+)$/.exec(jQuery('#amount').val()), val1, val2;
if (parts) {
    val1 = parts[1];
    val2 = parts[2];
} else {
    // there is an error in your string
}

You can also tolerate spaces here and there: /^\s*€\s*([0-9.]+)\s*-\s*€\s*([0-9.]+)\s*$/

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97