Skip to main content

How to Turn a snake_case or camelCase Name into Words in JavaScript

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Code names are written for code: reset_my_password, resetMyPassword, create-ticket. When a page has to show one to a person, Reset my password reads better. Two regular expressions and String.replace turn snake_case, camelCase, PascalCase and kebab-case names into words.

Syntax​

Syntax
const <words> = <name>
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
.replace(/[_-]+/g, " ")
.trim()
.toLowerCase();

const <label> = <words>.charAt(0).toUpperCase() + <words>.slice(1);
PartWhat it does
/([a-z0-9])([A-Z])/gFinds every place where a lowercase letter or a digit is followed by a capital letter: the border between two words in camelCase and PascalCase. The parentheses capture the two characters, and g finds all of them, not only the first
"$1 $2"Puts the two captured characters back with a space between them. MDN: $n Inserts the nth (1-indexed) capturing group
/[_-]+/g, " "Replaces every run of underscores and hyphens with one space: the borders in snake_case and kebab-case
trim()Removes spaces at the start and end, which a leading or trailing _ leaves behind
toLowerCase()Makes every word lowercase
charAt(0).toUpperCase() + slice(1)Capitalizes the first letter only, for sentence case

The Example​

A small script turns six code names into labels: snake_case, camelCase, PascalCase, kebab-case, a name with a version number, and one with an acronym.

Prerequisites​

  • Node.js 22.18 or later, so that node runs a TypeScript file directly. node --version shows yours; I used Node.js 26.

Step 1: Write the Function​

Create a folder, and in it a file named words.ts:

words.ts
// A code name in words: reset_my_password becomes "Reset my password".
function toWords(name: string): string {
const words = name
.replace(/([a-z0-9])([A-Z])/g, "$1 $2") // resetMyPassword -> reset My Password
.replace(/[_-]+/g, " ") // reset_my-password -> reset my password
.trim()
.toLowerCase();
return words.charAt(0).toUpperCase() + words.slice(1);
}

const names = [
"reset_my_password",
"resetMyPassword",
"ResetMyPassword",
"create-ticket",
"get_my_devices_v2",
"parseHTTPResponse",
];

for (const name of names) {
console.log(`${name.padEnd(20)} -> ${toWords(name)}`);
}

toWords is the syntax above as a function. padEnd(20) lines the output up in two columns.

Step 2: Run It​

Terminal
node words.ts
Output
reset_my_password -> Reset my password
resetMyPassword -> Reset my password
ResetMyPassword -> Reset my password
create-ticket -> Create ticket
get_my_devices_v2 -> Get my devices v2
parseHTTPResponse -> Parse httpresponse

macOS-style terminal window titled Terminal — name-to-words showing six names and their labels: reset_my_password, resetMyPassword and ResetMyPassword all become Reset my password, create-ticket becomes Create ticket, get_my_devices_v2 becomes Get my devices v2, and parseHTTPResponse becomes Parse httpresponse

  • The first three are the same name in three styles, and all become Reset my password.
  • create-ticket shows the hyphen rule, and get_my_devices_v2 the underscore rule with a number.
  • parseHTTPResponse goes wrong: Parse httpresponse. An acronym is a run of capitals, and the first rule only splits where a lowercase letter or digit meets a capital, so nothing splits HTTP from Response.

Step 3: Split Acronyms Too​

One more rule fixes the acronym: where a run of capitals is followed by a capital and a lowercase letter, the last capital starts a new word. Create acronyms.ts:

acronyms.ts
// The same, plus one rule: an acronym followed by a word, such as HTTPResponse, splits before the word.
function toWords(name: string): string {
const words = name
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2") // HTTPResponse -> HTTP Response
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
.replace(/[_-]+/g, " ")
.trim()
.toLowerCase();
return words.charAt(0).toUpperCase() + words.slice(1);
}

for (const name of ["parseHTTPResponse", "resetMyPassword", "get_my_devices_v2"]) {
console.log(`${name.padEnd(20)} -> ${toWords(name)}`);
}
Terminal
node acronyms.ts
Output
parseHTTPResponse -> Parse http response
resetMyPassword -> Reset my password
get_my_devices_v2 -> Get my devices v2

/([A-Z]+)([A-Z][a-z])/g turns HTTPResponse into HTTP Response before the other rules run, and the other names don't change. The acronym ends up lowercase, like every other word, because toLowerCase makes no exceptions.

The Complete File​

macOS-style code window titled words.ts showing the toWords function, which replaces lowercase-capital borders with a space, replaces underscores and hyphens with a space, trims, lowercases and capitalizes the first letter, followed by a list of six names and a loop that prints each name next to its label

words.ts from Step 1 is the whole program; acronyms.ts is the version that also splits acronyms.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out the g flagOnly the first border is replacedWrite /…/g
Lowercasing before splittingThe capitals that mark camelCase borders are goneSplit first, lowercase last
Writing "$1$2"The two characters go back without a spaceWrite "$1 $2"
Expecting acronyms to splitparseHTTPResponse becomes Parse httpresponseAdd the acronym rule from Step 3

FAQ​

How do I get Title Case instead?​

Capitalize every word instead of the first: split the words on spaces, capitalize each one the way toWords capitalizes the first, and join them again.

Why not a library?​

For labels, two rules are enough, and you can read them. A library makes sense when you also need to go the other way, from words to code names, in many styles.

Conclusion​

To turn a code name into words, put a space between a lowercase letter or digit and a following capital with /([a-z0-9])([A-Z])/g and "$1 $2", turn underscores and hyphens into spaces with /[_-]+/g, then trim, lowercase, and capitalize the first letter. For names with acronyms, split runs of capitals first with /([A-Z]+)([A-Z][a-z])/g.

Additional Resources​