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

206
Views
How to separate words in a sentences string in JavaScript without comma

Hello i want to ask how to separate words in a sentences string without commas, when the initial letter is capitalized from the previous letter then the word is separated

example 
let str = "WishYourLuck"

//expected output = Wish Your Luck

**thank you in advance

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

0

You can replace using regexp that captures word ends /([a-z])([A-Z])/g ie small letter followed by capital letter.

let str = "WishYourLuck"

console.log(str.replace(/([a-z])([A-Z])/g, (_, a, b) => `${a} ${b}`))

W/o replace

let str = "WishYourLuck"

console.log(splitWords(str))


function splitWords(str) {
  const out = []
  
  for (let i = 0; i < str.length; i++) {
    const char = str[i]
    const prevChar = str[i-1]
    if (i && char >= 'A' && char <= 'Z' && prevChar >= 'a' && prevChar <= 'z') {
      out.push(' ')
    }
    
    out.push(char)
  }
  
  return out.join('')
}

about 4 years ago · Juan Pablo Isaza Report

0

A little too late. Here is how to do it step by step.

const str = "WishYourLuck";
const arr = [...str];
let res = '';

arr.forEach((c) => {
  if (c == c.toLowerCase()) {        
    res += c; // The character is lowercase
  } else {
    res += ' ' + c; // The character is uppercase
  }  
})

console.log(res)

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!