Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
Adding separator to alphanumeric string using RegEx

I have a string as : A123456789BC

And I would like the output to be : A-123-456-789-BC for display purposes.

I am using a regex in replace function but so far it only does : A-123-456-789BC

This is what I have -

String.replace(/(\d{3})/g, (found) => {return '-' + found});

Basically if I have an alphanumeric string as above, I would like the numbers to be grouped in 3 and add separators(-) to their start and end.

Would appreciate any help to achieve this.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Add a match for the ending and you should have it. So match 3 numbers or two characters at the end.

console.log('A123456789BC'.replace(/(\d{3}|[A-Z]+$)/g, '-$1'))

about 4 years ago · Juan Pablo Isaza Report

0

I would update your regular expression like this:

String.replace(/(\d{3}|(?<=\d{3})\D)/g, (found) => {return '-' + found})

This will not only add a hyphen before a group of 3 digits, but also before a non-digit character following a group of 3 digits.

console.log('A123456789BC'.replace(/(\d{3}|(?<=\d{3})\D)/g, (found) => {return '-' + found}))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!