I have the following string A.b.c. This is an example
I would like to make it so that any letters which have a period immediately after them are capital.
This is the code I currently have, which works. Any ideas on how to make this more concise?
let testString = 'A.b.c. This is an example'
const pieces = testString.split('.')
let tempString = ''
let replaceString = ''
pieces.forEach(piece => {
if (piece.length === 1) {
tempString += piece.toUpperCase() + '.'
replaceString += piece + '.'
}
})
let newString = testString.replace(replaceString, tempString)
console.log(newString)
//A.B.C. This is an example
This can be done with a regular expression and a custom replacement function. It may look cleaner if you're not bothered by the ugliness of regular expressions.
let testString = 'A.b.c. This is an example.';
let newString = testString.replace(/\w\./g, letter => letter.toUpperCase());
console.log(newString);