• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Capitalize the first letter of every word in a string

Capitalize the first letter of every word in a string

Capitalize 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());

// EXAMPLE
capitalizeEveryWord('hello world!'); // 'Hello World!'

Source

https://www.30secondsofcode.org/js/s/capitalize-every-word

Miscellaneous

Related Snippets:

  • Convert a string to a number
  • Press shift and enter for a new line
  • Drop list elements from the right
  • How To Import A JavaScript File In Another JavaScript File

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers