我发现此示例将 CamelCase 更改为破折号。我已修改代码以将 CamelCase 更改为 Sentencecase,其中包含空格而不是破折号。它工作正常,但不适用于一个单词字母,如“i”和“a”。任何想法如何合并它?
thisIsAPain --> 这是一个痛苦
var str = "thisIsAPain"; str = camelCaseToSpacedSentenceCase(str); alert(str) function camelCaseToSpacedSentenceCase(str) { var spacedCamel = str.replace(/\W+/g, " ").replace(/([a-z\d])([A-Z])/g, "$1 $2"); spacedCamel = spacedCamel.toLowerCase(); spacedCamel = spacedCamel.substring(0,1).toUpperCase() + spacedCamel.substring(1,spacedCamel.length) return spacedCamel; }