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

146
Views
Regex add space in string if the word is longer than 4 characters and have numbers

I try to create a regex with 2 condition:

  • if word length more than 4 character
  • And if the word contains numbers

I need to add spaces

So like: iph12 return iph12, but iphone12 return iphone 12

I wrote regex

.replace(/\d+/gi, ' $& ').trim()

and this function return in anyway string like iphone 12. I tried to use function

.replace(/(?=[A-Z]+\d|\d+[A-Z])[A-Z\d]{,4}/i, ' $& ').trim()

but without second argument in {,4} it's not working. So is this possible?

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

0

You can use

text.replace(/\b([a-zA-Z]{4,})(\d+)\b/g, '$1 $2')

See the regex demo. Details:

  • \b - word boundary
  • ([a-zA-Z]{4,}) - Group 1: four or more ASCII letters
  • (\d+) - Group 2: one or more digits
  • \b - word boundary

See the JavaScript demo:

const texts = ['iphone12', 'iph12'];
const regex = /\b([a-zA-Z]{4,})(\d+)\b/g;
for (const text of texts) {
    console.log(text, '=>', text.replace(regex, '$1 $2'));
}

Output:

iphone12 => iphone 12
iph12 => iph12
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!