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
How do I convert lowercase and underscores to proper case and spaces in ES6/React

How do I convert strings with underscores into spaces and converting it to proper case?

CODE

 const string = sample_orders

 console.log(string.replace(/_/g, ' '))

Expected Output

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

0

This function should do the job :

function humanize(str) {
  var i, frags = str.split('_');
  for (i=0; i<frags.length; i++) {
    frags[i] = frags[i].charAt(0).toUpperCase() + frags[i].slice(1);
  }
  return frags.join(' ');
}


humanize('sample_orders');
// > Sample Orders

JSFiddle

about 4 years ago · Juan Pablo Isaza Report

0

.replace replace only the first occurrence of the target input. In order to replace all the occurrences, use .replaceAll.

 var string = 'sample_orders'

 string = string.replaceAll('_', ' ')

Further converting it to the proper case could be accomplished through regEx

string = string.replace(/(^\w|\s\w)/g, firstCharOfWord => firstCharOfWord.toUpperCase());

Where:

  • ^\w : First char of the string
  • | : or
  • \s\w : First char after whitespace
  • g : global scope(Match all occurrences)
  • The second argument is a function that accepts the first character of each word computed using the regular expression and converts it to an upper case. Binding the above logic into a function:
    function formatString(string){
     
     return string.replaceAll('_', ' ').replaceAll(/(^\w|\s\w)/g, firstCharOfWord => firstCharOfWord.toUpperCase());
    
    }
    
    let formattedString = formatString('sample_orders')
    console.log(formattedString) //Sample Orders
    

    Caveat: You might encounter an error saying .replaceAll is not a function if you are running on an older browser or runtime environment. The .replaceAll method was added in ES2021/ES12. The best workaround to run such functionalities on older versions of JS is to provide native support to older versions that do not support newly added methods or features ( .replaceAll) in this case.

    function formatString(string){
    
     return string.replace(/_/g, ' ').replace(/(^\w|\s\w)/g, firstCharOfWord => firstCharOfWord.toUpperCase());
    
    }
    let formattedString = formatString('sample_orders')
    console.log(formattedString) //Sample Orders
    

    Note that we're using the replace method with regular expressions on a global scope. This could also be used at instances where the strings might not contain underscores.

    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!