I haven't done a lot of work with regex, and I'm getting stuck. I'm trying to take a string and make it title case, but with some exceptions. I also want to remove any whitespace.
Currently it's removing whitespace and the title case is working, but it's not following the exceptions. Is there a way to combine the "title" variable with "regex" variable, and make it so the exceptions are working?
const toTitleCase = str => {
const title = str.replace(/\s\s+/g, ' ');
const regex = /(^|\b(?!(AC | HVAC)\b))\w+/g;
const updatedTitle = title
.toLowerCase()
.replace(regex, (s) => s[0].toUpperCase() + s.slice(1));
return updatedTitle;
}
console.log(toTitleCase(`this is an HVAC AC converter`))