-1

I am attempting to correct a grammar error from an API. I'm attempting to replace 70,2bn to 70.2bn I've went about it to just simply to .replace(/,[0-9]bn/g, '.') but this ends up removing the last number after the comma.

Here is an example of the full string "The average population is 492,213 and revenue generated is 70,2bn"

With the current .replace(/,[0-9]bn/g, '.') it is replacing the comma with a period which I am expecting, but how can I return back the number that is cut off?

This number 70,2bn is different per page, so I attempting to find a solution that will work for different scenarios that include bn such as 2,1bn, 100,3bn etc.

Here is a code snippet:

let string = "The average population is 492,213 and revenue generated is 70,2bn"
console.log(string)


string = string.replace(/,[0-9]bn/g, '.bn')
console.log(string)

I am expecting an outcome of "The average population is 492,213 and revenue generated is 70.2bn"

1
  • 2
    string.replace(/,([0-9]bn)/g, '.$1') Commented Jun 1, 2021 at 19:55

1 Answer 1

-1

Try this:

> '70,2bn'.replace(/(\d+),(\d+bn\b)/g, '$1.$2')
"70.2bn"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.