DEV Community

loading...
Cover image for Day 41: String Case

Day 41: String Case

mattryanmtl profile image Matt Ryan ใƒป1 min read

Title Case

function toTitleCase(str) {
  return str.replace(
    /\w\S*/g,
    function(txt) {
      return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    }
  );
}
Enter fullscreen mode Exit fullscreen mode

Camel Case

function camelize(str) {
  return str.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) {
    if (+match === 0) return ""; // or if (/\s+/.test(match)) for white spaces
    return index === 0 ? match.toLowerCase() : match.toUpperCase();
  });
}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Forem Open with the Forem app