最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add a space between camel case key name? - Stack Overflow

programmeradmin1浏览0评论

I've created a node.js website that potential employees can apply for a job through. When they submit the application form I am using nodemailer and mailgun to send an email to the hiring manager with the applicants information. It sends the object of the new applicant with the key value pairs, but I would like to put a space between the keys containing more than one word and make them uppercase to be a little more eye pleasing. How can I do this?

here is a sample email with output key value pairs

Here is the code for sending an email

function sendAppliedEmail(applicant) {


let html = '<img src="" alt="logo">';
  html += '<h2 style="color: black">New Applicant</h2>'
  html += '<ul>';

  Object.entries(applicant).forEach(([key, value]) => {
    html += `<li>${key}: ${value}</li>`;
  });

  html += '</ul>';

I've created a node.js website that potential employees can apply for a job through. When they submit the application form I am using nodemailer and mailgun to send an email to the hiring manager with the applicants information. It sends the object of the new applicant with the key value pairs, but I would like to put a space between the keys containing more than one word and make them uppercase to be a little more eye pleasing. How can I do this?

here is a sample email with output key value pairs

Here is the code for sending an email

function sendAppliedEmail(applicant) {


let html = '<img src="" alt="logo">';
  html += '<h2 style="color: black">New Applicant</h2>'
  html += '<ul>';

  Object.entries(applicant).forEach(([key, value]) => {
    html += `<li>${key}: ${value}</li>`;
  });

  html += '</ul>';
Share Improve this question asked Mar 28, 2019 at 17:25 klaurtarklaurtar 2537 silver badges23 bronze badges 3
  • Here is an answer on converting camel case: stackoverflow./questions/7225407/… – kht Commented Mar 28, 2019 at 17:28
  • Did you want the initial character capitalized as well? – zfrisch Commented Mar 28, 2019 at 17:34
  • stackoverflow./questions/18379254/regex-to-split-camel-case – epascarello Commented Mar 28, 2019 at 17:34
Add a ment  | 

3 Answers 3

Reset to default 7
replace(/([a-z])([A-Z])/g, `$1 $2`)


You can replace all lowerUpper matches.

Everytime a lowercase character followed by a uppercase character, the regex match and replace the both characters with themself and a space between.

function sendAppliedEmail(applicant) {


let html = '<img src="" alt="logo">';
  html += '<h2 style="color: black">New Applicant</h2>'
  html += '<ul>';

  Object.entries(applicant).forEach(([key, value]) => {
    html += `<li>${key.replace(/([a-z])([A-Z])/g, `$1 $2`)}: ${value}</li>`;
  });

  html += '</ul>';


if the key should be plete uppercase you can chain the toUpperCase() method to the replace method.

html += `<li>${key.replace(/([a-z])([A-Z])/g, `$1 $2`).toUpperCase()}: ${value}</li>`;

You can use the replace function to ascertain if a letter is upper case and return a prefixed space with the character if it is, while simultaneously making sure that the first character is capitalized.

key.replace(/[a-z]/gi, (m, o) => (m < {} && o) ? ` ${m}` : (o) ? m : m.toUpperCase())

Brief Explanation

You can determine whether or not a character is capitalized by paring it to {}. If a character is less than it is capitalized, and if it is greater than it is lower case. This is because {} will be transformed to the string [object Object], and both the character codes will be pared. Upon parison only the first character will be used("["). Thus "A" < "[" and "A" < {} is equivalent. This is easier explained through seeing their character codes:

"A".charCodeAt(0);
 //65
"[".charCodeAt(0);
 //91
"a".charCodeAt(0);
 //97

(In fact, it's more performant to use "[", and for optimization you would pare to that instead. It's simply easier to remember "{}" is greater than capital characters and vice versa)

Why do this?

In typical RegEx you could simply use [A-Z] to grab capitalized characters, but the caveat is that if you're looking to capitalize the first character AND space camel case, this parison in addition to the offset parameter( think of it as an index of the string ) in the replace function allows you to do so in a singular pass through the string.


An Example:

let splitCase = key => key.replace(/[a-z]/gi, (m, o) => (m < "[" && o) ? ` ${m}` : (o) ? m : m.toUpperCase());

let key = "phoneNumber";
console.log(splitCase(key));

key = "firstName";
console.log(splitCase(key));


In Your Code:

The below snippet is non-functioning, but it should work within your code:

function sendAppliedEmail(applicant) {
let splitCase = (key) => key.replace(/[a-z]/gi, (m, o) => (m < "[" && o) ? ` ${m}` : (o) ? m : m.toUpperCase());

let html = '<img src="" alt="logo">';
  html += '<h2 style="color: black">New Applicant</h2>'
  html += '<ul>';

  Object.entries(applicant).forEach(([key, value]) => {
    html += `<li>${splitCase(key)}: ${value}</li>`;
  });

  html += '</ul>';

You could slit them on the uppercase character with a regex. See this question: Javascript Split string on UpperCase Characters

发布评论

评论列表(0)

  1. 暂无评论