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?
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?
Share Improve this question edited Jul 24, 2014 at 11:46 DGibbs asked Jul 24, 2014 at 11:24 DGibbsDGibbs 14.6k7 gold badges49 silver badges88 bronze badges5 Answers
Reset to default 11replace
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,'');
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
.
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
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
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, '')