How do I output a number from a currency value using regex in javascript
EDIT 2
Since I’ve solved this on my own with no help from the community and it seems unlikely that this question will be re-opened; in case you’ve arrived here looking for a solution, here it is:
I was close on the previous edit; just had to use .match()
on the string to apply the regex. Here’s the full method (that works):
function removeCurrencyFormat(v) { const regex = /\d[0-9,]*\d[.]/; const part = v.match(regex); // Outputs ["40,000."] // Remove formatting artifacts that we don't need when // returning a value that can be used as a number type. const output = part[0].replace(",", "").replace(".", ""); return output; }
Thanks for nothing once again, SO. Always a pleasure wasting my time here.
EDIT
The duplicate deals with replacing the matched regex with something else. This is not what I want to do. I’m trying to do the opposite.
I have my regex that matches the value that I want to extract from my input string.
It’s worth noting at this point that the regex has changed to match the part of the string that I want to take out.
function removeCurrencyFormat(v) { const regex = /\d[0-9,]*\d[.]/; // Apply the regex to the input string to extract "40,000." const part = ??? // Output "40000" const output = part.replace(',', '').replace('.', ''); return output; }
The method applies the regex to extract a usable part of the string being the sought after value and replaces superfluous characters.
I just don’t know how to put the part of the string that the regex matches into its own variable.
The duplicate answer replaces the regex match with something else; this isn’t what I’m looking to do.
ORIGINAL QUESTION
My input is a currency value; here are some samples that my regex will match:
$100.00
€40,000.00
kr9,000,000.00
The regex matches a currency symbol of no more than 2 characters and a value of no less than 100 and follows:
/^.{2}?\d[0-9,]*\d\.?\d{0,2)$/
What I don’t understand is how I can use this regex to output values that I can use as number data types. Output that I’m looking for based on the examples above are:
100
40000
9000000
function removeCurrencyFormat(v) { const regex = /^.{2}?\d[0-9,]*\d\.?\d{0,2)$/; }
That’s literally as far as I can figure on going.
How do I extract the numbers from the currency values from here?
Thanks in advance!