camel case first javascript 1

camel case first javascript

const country = 'united kingdom'

const countryArr = country.split(' ')
const newcountry = countryArr
	.map((val) => {
		const pattern = new RegExp(`^[${val}]`, 'i')
		return val.replace(pattern, `${val.charAt(0).toUpperCase()}`)
	})
	.join(' ')
	.trim()

console.log(newcountry)

Here is what the above code is Doing:
1. We split the string into an array of words
2. We map over the array and replace the first letter of each word with its uppercase equivalent
3. We join the array back into a string
4. We trim the string to remove any whitespace

Similar Posts