somevar = ' 150,00 $';
someothervar = '$25.0;
I want to remove dollar signs, spaces, mas or dots.
Tried somevar.replace(/('$'|' '|,|\.)/g,'');
returned 15000 $
(leading space).
So it looks like the ma 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?
somevar = ' 150,00 $';
someothervar = '$25.0;
I want to remove dollar signs, spaces, mas or dots.
Tried somevar.replace(/('$'|' '|,|\.)/g,'');
returned 15000 $
(leading space).
So it looks like the ma 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?
Share Improve this question asked Nov 19, 2015 at 23:27 Doug FirDoug Fir 21.4k54 gold badges192 silver badges341 bronze badges 3-
What is the expected output?
150,00 $
->150
or15000
? – Wiktor Stribiżew Commented Nov 19, 2015 at 23:30 - are you pletely sure about dots and mas? – RobertoNovelo Commented Nov 19, 2015 at 23:30
-
/\D/g
is the best way, but be careful, one day if your occasionally remove the decimals from your response... ;) – Roko C. Buljan Commented Nov 19, 2015 at 23:40
2 Answers
Reset to default 2You 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"
I would:
var somePriceString = "$$2.903.5,,,3787.3";
console.log(somePriceString.replace(/\D/g,''));
If I wanted to remove any non-digit character.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744975708a4604130.html
评论列表(0条)