Replace underscores with spaces and capitalize words

replace space with underscore javascript
javascript capitalize first letter
replace spaces with underscores regex
replace white space with underscore js
javascript string replace all underscore with space
javascript'' ; // capitalize first letter of each word
regex space to underscore
replace empty space with

I am attempting to create a way to convert text with lowercase letters and underscores into text without underscores and the first letter of each word is capitalized.

ex;

options_page = Options Page

At this page: How to make first character uppercase of all words in JavaScript?

I found this regex:

key = key.replace(/(?:_| |\b)(\w)/g, function(key, p1) { return p1.toUpperCase()});

This does everything except replace the underscores with spaces. I have not really tried anything because I am not that familiar with regexpressions.

How can I adjust this regex so it replaces underscores with spaces?

This should do the trick:

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('humpdey_dumpdey');
// > Humpdey Dumpdey

repl

http://repl.it/OnE

Fiddle:

http://jsfiddle.net/marionebl/nf4NG/

jsPerf:

Most test data: http://jsperf.com/string-transformations

All versions plus _.str: http://jsperf.com/string-transformations/3

Change case of string, replace hyphens with spaces and capitalize , Some things: That pretty variable is pretty useless. Omit it. temp is quite meaningless. Use a descriptive name like words or parts . You could use the Array map  Underscored Text Generator. ToggleCase cuts out all the hassle of manually replacing spaces with underscores to create Embedded Underscore text. Simply type or paste any words you want to convert into the form below, and hit the magic Underscored Text button. Then just copy and paste your Embedded Underscore text to use!

These are two different tasks, so two different regexes is the best solution:

key = key.replace(/_/g, ' ').replace(/(?: |\b)(\w)/g, function(key) { return key.toUpperCase()});

To ensure even all capital words is processed. You can add .toLowerCase() before the very first .replace:

console.log('TESTING_WORD'.toLowerCase().replace(/_/g, ' ')
.replace(/(?: |\b)(\w)/g, function(key, p1) {
    return key.toUpperCase();    
}));

U-Convert: Online lowercase and uppercase text converter, replace , Capitalize every word option changes the case of all text to lower case while Compact spaces options replaces all occurrences of two or more spaces with one  \$\begingroup\$ So stumbled upon this searching on how to capitalize and replace hyphens and underscores from a string. Have been trying to modify the regex to match underscores as well for a while now, but no luck. Any possibility of modifying the regex solution to replace underscores as well ?

Since Lodash 3.1.0, there's a _.startCase([string='']) method that transforms any case into capitalized words (start case):

_.startCase('hello_world'); // returns 'Hello World'
_.startCase('hello-world'); // returns 'Hello World'
_.startCase('hello world'); // returns 'Hello World'

There are other useful methods in the String section of Lodash. Read the documentation here.

Replacing spaces with underscores in JavaScript, Return value: It returns a new Array, having the splitted items. Example 1: This example replaces all spaces(' ') with underscores(“_”) by using replace() method. Press Ctrl+H (Find and Replace Shortcut Key). In Find and Replace dialog box look for “ Find what ” option and provide one space and in “ Replace with ” dialog box provide underscore (_) and Click on “ Replace All ” button.

Here:

var str = 'Lorem_ipsum_dolor_sit_amet,_consectetur____adipiscing_elit.'
str = str.replace(/_{1,}/g,' ').replace(/(\s{1,}|\b)(\w)/g, function(m, space, letter)
{
  return space + letter.toUpperCase();
})

console.log(str);

Collect, Combine, and Transform Data Using Power Query in Excel , For example, you might want to replace all underscore characters with space characters or capitalize each word in every column name. Simple text manipulation  This method will apply the Find and Replace feature to replace blank spaces with nothing or underscore/dash/comma from selected cells easily. 1. Select the range you will replace the blank spaces, and press the Ctrl + H keys at the same time to open the Find and Replace dialog box.

Another alternative:

camel = "options_page".replace(/(^|_)(\w)/g, function ($0, $1, $2) {
  return ($1 && ' ') + $2.toUpperCase();
});
console.log(camel);

freeCodeCamp Challenge Guide: Spinal Tap Case - Guide, Convert the given string to a lowercase sentence with words joined by dashes. Create a regular expression for all white spaces and underscores. just turn the uppercase to lowercase and replace spaces with underscores using replace() . To capitalize the first letter of each word, in addition to replacing underscores, change the return line to this: return ucwords(trim(str_replace('_', ' ', $value))); Basically it’s the strtolower() function that converts the string to lowercase, so by replacing it with ucwords(), we ensure that each word is capitalized.

freeCodeCamp Algorithm Challenge Template Guide - Guide, Convert the given string to a lowercase sentence with words joined by dashes. a variable for the white space and underscores. var regex = /\s+|_+/g; // Replace low-upper The first replace() puts a space before any encountered uppercase  This simple function will find only those words you wish to leave in lowercase (or uppercase), and do so with the speed of str_replace instead of any regular expressions. When run, the str_ireplace case insensitivity only applies to the search.

Use Notepad++ to change under_score_case to CamelCase , Add a placeholder character to the front of each word, I chose Z . It probably doesn't have to be alphabetic, Replace underscores with spaces. with NP++ for a bit), Notepad++ does not understand the uppercase macro \u in Perl Regexp. I want to make one formula that takes the text string of another cell and randomly replaces one word with an underscore ____. I'd also like to make a formula that replaces all the words with underscores. Examples below. [I know it's possible to find words by counting spaces, my bigger challenge replace the found words.]

Python Exercises: Replace whitespaces with an underscore and , In case there are both spaces and underscores in the string to start. Guess it wouldn't work though if there were some @@@ already in the string,  If a word starts not with a letter, it shall remain intact too. Assume that the parameter String can only contain spaces and alphanumeric characters. NOTE: please keep in mind that the words can be divided by single or multiple spaces.The spaces also can be found at the beginning or the end of the parameter string,and you need to preserve them.

Comments
  • For those who are using lodash, there is startCase function for that
  • I'm with you concerning code size but according to my tests str.split is way faster in most browsers.
  • Code size is 131 vs 111 bytes when uglified and defined as function humanize.
  • Interesting result! I'll need to revise my practices.
  • 20 times faster. Nice.
  • Instantly readable/understandable code, and faster than regex. Who would have thought it possible?
  • You can remove the underscore from the second regexp, since they've all been removed.
  • Thanks for the answer. Unfortunately, as is, it is producing the same results as before; contact_details = ContactDetails; It is not putting the space in there.
  • Right, I didn't notice that the second regex also removes spaces. I've edited the code to change that functionality. Now it works as intended, you can check out here.
  • To make it better you can add key.toLowerCase() before the very first key.replace to ensure even all capital words is processed.
  • sidenote: if you want to import only this one function instead of the entire library in react you can use import startCase from 'lodash/startCase' at the top of your file instead of importing the entire library if you're only using it for this one thing.