【发布时间】:2012-11-23 02:28:33
【问题描述】:
我已找到 this example 将 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; }
【问题讨论】:
-
这是您的第 12 个问题(除了 17 个答案),您现在应该正确格式化内容。当您输入问题时,右侧有一个方便的 如何格式化 框。下面还有一个预览区。
-
哎呀!那是我的错误。我知道代码格式化,但我必须记得在代码顶部添加新行,然后再用 "\n "
标签: javascript regex camelcasing