Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a space between camel case key name?

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

enter image description here

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>';
like image 637
klaurtar Avatar asked Oct 21 '25 06:10

klaurtar


1 Answers

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 complete 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>`;
like image 122
d-h-e Avatar answered Oct 22 '25 19:10

d-h-e