如何在 JavaScript 中将“helloThere”或“HelloThere”之类的字符串转换为“Hello There”?
最佳答案
const text = 'helloThereMister';
const result = text.replace(/([A-Z])/g, " $1");
const finalResult = result.charAt(0).toUpperCase() + result.slice(1);
console.log(finalResult);
首字母大写——举个例子。注意 "$1"
中的空格。
当然,如果第一个字母已经是大写 - 您将有多余的空间可以删除。
关于javascript - 将驼峰式文本转换为首字母大写文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7225407/