4

I'm attempting to remove all instances of a given set of characters £$€,. from a string in jQuery/Javascript. I'm using the replace function, however this only appears to remove a single instance of the character and not all of them.

For example consider the string:

1,500,00.00.$djdjd£10€10

I get back:

1500,0000.djdjd1010

As you can see, it only removes a single instance of each character. £, $ and are fine as there is only one of each in the string.

Here is what I have so far:

function validatePriceRange(value, min, max) {

    var replacements = ["£", "$", "€", ",", "."];

    $.each(replacements, function (index, item) {
        value = value.replace(item, "");
    });

    var value = parseInt(value, 10);

    return value >= min && value <= max;
}

jsFiddle

Can anyone spot what I've done wrong?

DGibbs
  • 14,316
  • 7
  • 44
  • 83

5 Answers5

11

replace called with a string as first argument does only one replacement, while using a regular expression with flag g replaces all occurrences.

Using a regular expression, you can also avoid looping over an array and do it in one pass :

value = value.replace(/£|\$|€|,|\./g,'');
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Perfect - thanks for the explanation on how replace works. I ignorantly assumed it worked in a similar way to how .`Replace()` works in `C#`. – DGibbs Jul 24 '14 at 11:40
1

You are only cycling through your replacement array once and replace everytime the specific character. But replace is only replacing the first occurance of a given string.

For a replace all method, look here.

Community
  • 1
  • 1
Florian Leitgeb
  • 15,657
  • 6
  • 31
  • 40
  • 1
    yay, pointing at existing solutions rocks. I might consider extending that solution by making ReplaceAll a prototype of String. – gillyspy Jul 24 '14 at 11:41
1

I don't think you need a function for it:

var validated = parseInt('1,500,00.00.$djdjd£10€10'.replace(/[£$€,.]/g,''), 10);
//=> 15000000
// or if you want the validated directly
var validated = function(min,max) {
                  var v = parseInt('1,500,00.00.$djdjd£10€10'
                                    .replace(/[£$€,.]/g,''), 10);
                  return v >= min && v <==max;
                }(1000, 200000); //=> false

The regular expression should be different if you want to include all digits in the string:

var validated = function(min,max) {
                  var v = parseInt('1,500,00.00.$djdjd£10€10'
                                    .replace(/[^\d]/g,''), 10);
                  //                           ^ replace non numbers
                  // v now is 150000001010
                  return v >= min && v <==max;
                }(1000, 200000); //=> false
KooiInc
  • 119,216
  • 31
  • 141
  • 177
0

Use a regex with the global flag, which will search and replace all instances

var replacements = ["£", "\\$", "€", ",", "\\."];
$.each(replacements, function (index, item) {
    value = value.replace(new RegExp(item, "g"), '');
});

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

As already answered here you could use the following regex that replaces all non characters and whitespaces with empty.

var value = "1,500,00.00.$djdjd£10€10"
value = value.replace(/[^\w\s]/gi, '')
Community
  • 1
  • 1
Jarno Lonardi
  • 303
  • 1
  • 7