0
"12,56,7abc,fgh" ===> "12567abc,fgh"
"1,245abc,1a" ===> "1245abc,1a"
"1,2,3,4,5abc" ===> "12345abc"

How to remove comma in (number),(number) format?

For the test

const array = ["12,56,7abc,fgh", "1,245abc,1a", "1,2,3,4,5abc"]
const answer = ["12567abc,fgh", "1245abc,1a", "12345abc"]

function formatter(string){
   // here
   return // string.replace('', '') ??
}

array.map((el, i) => { 
   const formatted = formatter(el) 
   if(answer[i] === formatted){
      console.log('Success. formatted: ', formatted)
   } else {
     console.log('Failed. formatted: ', formatted)
   }
})
7
  • 5
    .replace(/(\d),(\d)/g,'$1$2') Commented Jun 10, 2020 at 23:53
  • Does this answer your question? Remove all occurrences except last? Commented Jun 10, 2020 at 23:54
  • @NiettheDarkAbsol Thanks but not working to last string Commented Jun 10, 2020 at 23:55
  • 1
    Fine. .replace(/(\d),(?=\d)/g,'$1') Commented Jun 10, 2020 at 23:56
  • What is the logic for "12345abc" result? How exactly is it different from "12567abc,fgh"? Commented Jun 10, 2020 at 23:56

1 Answer 1

2
str.replace(/(?<=\d),(?=\d)/g, '')

(?<=\d): lookbehind - a digit

(?=\d) : lookahead - a digit

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.