I have a sentence with a colon in the middle, and want to capitalize the first word after the colon, I would like to know how to replace the matching letter with its’ upper case using regexp in javascript, however xx.replace(/(:\s\w)/, '$1'.toUpperCase()) doesn’t work, it returns Lactobacillus strains: a BIOPEP-UWM database-based analysis. Expect Lactobacillus strains: A BIOPEP-UWM database-based analysis.
MWE:
var xx = 'Lactobacillus strains: a BIOPEP-UWM database-based analysis';
xx.replace(/(:\s\w)/, '$1'.toUpperCase());
Any suggestion is appreciated.
>Solution :
All you need to do is use a callback function as the second argument instead of '$1'
var xx = 'Lactobacillus strains: a BIOPEP-UWM database-based analysis';
console.log(xx.replace(/:\s\w/, fullMatch => fullMatch.toUpperCase()));
What you were doing was capitalizing the literal value '$1', which is the same, so it was just passing '$1' into the replace function