Files
30-seconds-of-code/snippets/js/s/capitalize-every-word.md
2023-05-07 16:07:29 +03:00

512 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Capitalize every word snippet javascript
string
regexp
laptop-plants 2020-10-22T20:23:47+03:00

Capitalizes the first letter of every word in a string.

  • Use String.prototype.replace() to match the first character of each word and String.prototype.toUpperCase() to capitalize it.
const capitalizeEveryWord = str =>
  str.replace(/\b[a-z]/g, char => char.toUpperCase());
capitalizeEveryWord('hello world!'); // 'Hello World!'