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

266
Views
Capitalize the first character of most words except some exceptions using javascript

I have the following JavaScript regex which capitalizes the first character of most words except some exceptions:

There are two issues with this regex that I have not resolved. The very first letter should always be capitalized. The second letter of every word should be lower case.

Here is an example of how it is working:

function capitalizeFirst(str) {
  return str.replace(/(?!^)\b(?!(?:of|the)\b)([a-z])/g, m => m.toUpperCase());
}


console.log(capitalizeFirst('the fox JUMP off of THE street'));

Where it should be returning this: ('of' and 'the' should be lower case except if on first word). Also it should lower case the rest of the word:

The Fox Jump Off of the Street

I could not find an answer to this specific question during a search.

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

0

I was able to figure out the solution. This is an explanation of fix:

  • The toLowerCase was added in the beginning to lower all characters
  • The regex was modified accordingly to handle this:
    enter image description here https://regex101.com/

function capitalizeFirst(str) {
  return str.toLowerCase()
            .replace(/\b(?!(?!^)(?:of|the)\b)([a-z])/g, m => m.toUpperCase());
}

console.log(capitalizeFirst('the fox JUMP off of THE street'));

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!