-1

I have these strings with numbers

"$12.3"
"12,3 SEK"
"12 pounds"

In all the occurrences i need to remove the number, float or not, and just keep the rest of the string.

"$"
"SEK"
"pounds"

I found several posts that are similar to my question, like this one: Removing Numbers from a String using Javascript

Where something like this is suggested in a comment:

"12.3 USD".replace(/\d+([,.]\d+)?/g)

But that only returns (with the Chrome dev console):

"undefined USD"
Adrian Rosca
  • 6,922
  • 9
  • 40
  • 57

1 Answers1

7

That's because you aren't telling it what to replace those values with. Try

"12.3 USD".replace(/\d+([,.]\d+)?/g, '')
//   replace with an empty string ---^

It looks like you also want to remove any whitespace coming after the numbers so you could modify your regex a bit to do that.

let result = "12.3 USD".replace(/\d+([,.]\d+)?\s*/g, '');
//                       remove whitespace ---^
console.log(result);
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91