Javascript 在Javascript中将字符串转换为Pascal Case(又名UpperCamelCase)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4068573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert string to Pascal Case (aka UpperCamelCase) in Javascript
提问by Blowsie
Id like to know how I can covert a string into a pascal case string in javascript (& most probally regex).
我想知道如何将字符串转换为 javascript 中的 pascal case 字符串(并且很可能是正则表达式)。
Conversion Examples:
转换示例:
- double-barrel = Double-Barrel
- DOUBLE-BARREL = Double-Barrel
- DoUbLE-BaRRel = Double-Barrel
- double barrel = Double Barrel
- 双桶 = 双桶
- DOUBLE-BARREL = 双桶
- DoUbLE-BarRRel = 双桶
- 双桶 = 双桶
Check this linkfor further info on Pascal Case
查看此链接以获取有关 Pascal Case 的更多信息
回答by Kobi
s = s.replace(/(\w)(\w*)/g,
function(g0,g1,g2){return g1.toUpperCase() + g2.toLowerCase();});
The regex finds words (here defined using \w
- alphanumerics and underscores), and separates them to two groups - first letter and rest of the word. It then uses a function as a callback to set the proper case.
正则表达式查找单词(此处使用\w
字母数字和下划线定义),并将它们分成两组 - 第一个字母和单词的其余部分。然后它使用一个函数作为回调来设置正确的情况。
Example: http://jsbin.com/uvase
Alternately, this will also work - a little less regex and more string manipulation:
或者,这也可以工作 - 少一点正则表达式和更多的字符串操作:
s = s.replace(/\w+/g,
function(w){return w[0].toUpperCase() + w.slice(1).toLowerCase();});
I should add this isn't pascal case at all, since you have word barriers (helloworld
vs hello-world
). Without them, the problem is almost unsolvable, even with a dictionary. This is more commonly called Title Case, though it doesn't handle words like "FBI", "the" or "McDonalds".
我应该补充一点,这根本不是 pascal 大小写,因为您有单词障碍(helloworld
vs hello-world
)。没有它们,即使使用字典,问题也几乎无法解决。这通常被称为 Title Case,尽管它不处理诸如“FBI”、“the”或“McDonalds”之类的词。
回答by kalicki2k
Here's my suggestion:
这是我的建议:
function toPascalCase(string) {
return `${string}`
.replace(new RegExp(/[-_]+/, 'g'), ' ')
.replace(new RegExp(/[^\w\s]/, 'g'), '')
.replace(
new RegExp(/\s+(.)(\w+)/, 'g'),
(, , ) => `${.toUpperCase() + .toLowerCase()}`
)
.replace(new RegExp(/\s/, 'g'), '')
.replace(new RegExp(/\w/), s => s.toUpperCase());
}
or
或者
String.prototype.toPascalCase = function() {
return this
.replace(new RegExp(/[-_]+/, 'g'), ' ')
.replace(new RegExp(/[^\w\s]/, 'g'), '')
.replace(
new RegExp(/\s+(.)(\w+)/, 'g'),
(, , ) => `${.toUpperCase() + .toLowerCase()}`
)
.replace(new RegExp(/\s/, 'g'), '')
.replace(new RegExp(/\w/), s => s.toUpperCase());
};
Test cases:
测试用例:
describe('String to pascal case', function() {
it('should return a pascal cased string', function() {
chai.assert.equal(toPascalCase('foo bar'), 'FooBar');
chai.assert.equal(toPascalCase('Foo Bar'), 'FooBar');
chai.assert.equal(toPascalCase('fooBar'), 'FooBar');
chai.assert.equal(toPascalCase('FooBar'), 'FooBar');
chai.assert.equal(toPascalCase('--foo-bar--'), 'FooBar');
chai.assert.equal(toPascalCase('__FOO_BAR__'), 'FooBar');
chai.assert.equal(toPascalCase('!--foo-??-bar--121-**%'), 'FooBar121');
});
});