【发布时间】:2015-09-21 06:15:48
【问题描述】:
我需要将一个字母数字字符串转换为普通字符串,它是用驼峰式命名的。我的输入可以是以下任何字符串:
var strs = {
input: [
"thisStringIsGood",
"somethingLikeThis",
"a123CapsHere345AndHEREButNOT678Here90End",
"123CapsHere345AndHEREButNOT678Here90e",
"ABCAcryonym",
"xmlHTTPRequest",
"thisDeviceHasA3PrintingService"
],
expectedResult: [
"This String Is Good",
"Something Like This",
"A123 Caps Here345 And HERE But NOT678 Here90 End",
"123 Caps Here345 And HERE But NOT678 Here90 e",
"ABC Acryoynm",
"Xml HTTP Request",
"This Device Has A3 Printing Service"
]
};
在将字符串转换为预期的格式之后,我正在这样做:
function capSplit(str) {
return str.replace(/(^[a-z]+)|[0-9]+|[A-Z][a-z]+|[A-Z]+(?=[A-Z][a-z]|[0-9])/g, function (match, first) {
if (first) match = match[0].toUpperCase() + match.substr(1);
return match + ' ';
})
}
但问题是空格也添加了数字。我需要将字符串"thisDeviceHasA3Service" 转换为"This Device Has A3 Printing Service" 但函数capSplit(str) 给了我This Device Has A 3 Printing Service 我不知道我缺少什么。这是fiddle
【问题讨论】:
-
为什么
"e""123 Caps Here345 And HERE But NOT678 Here90 e",不大写?
标签: javascript regex