Reputation: 21232
somevar = ' 150,00 $';
someothervar = '$25.0;
I want to remove dollar signs, spaces, commas or dots.
Tried somevar.replace(/('$'|' '|,|\.)/g,'');
returned 15000 $
(leading space).
So it looks like the comma is being removed but not everything else?
I could go like this:
somevar.replace(/\$/g,'').replace(/ /g,'').replace(/,/g,'')
But surely there's a more 'elegant' way?
Upvotes: 4
Views: 6293
Reputation: 3809
I would:
var somePriceString = "$$2.903.5,,,3787.3";
console.log(somePriceString.replace(/\D/g,''));
If I wanted to remove any non-digit character.
Upvotes: 2
Reputation: 240958
You could use /[$,.\s]/g
:
' 150,00 $'.replace(/[$,.\s]/g, '');
// "15000"
'$25.0'.replace(/[$,.\s]/g, '');
// "250"
Your regular expression wasn't working because you needed to escape the $
character, and remove the single quotes. You could have used: /\$| |,|\./g
.
Alternatively, you could also just replace all non-digit character using /\D/g
:
' 150,00 $'.replace(/\D/g, '');
// "15000"
'$25.0'.replace(/\D/g, '');
// "250"
Upvotes: 2