简体   繁体   English

在 Javascript 中将字符串转换为 Pascal Case(又名 UpperCamelCase)

[英]Convert string to Pascal Case (aka UpperCamelCase) in Javascript

Id like to know how I can covert a string into a pascal case string in javascript (& most probally regex).我想知道如何将字符串转换为 javascript 中的帕斯卡大小写字符串(很可能是正则表达式)。

Conversion Examples:转换示例:

  • double-barrel = Double-Barrel双桶 = 双桶
  • DOUBLE-BARREL = Double-Barrel双桶 = 双桶
  • DoUbLE-BaRRel = Double-Barrel DoUbLE-BaRRel = 双桶
  • double barrel = Double Barrel双桶 = 双桶

Check this link for further info on Pascal Case检查此链接以获取有关 Pascal Case 的更多信息

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.正则表达式查找单词(此处使用\w - 字母数字和下划线定义),并将它们分成两组 - 第一个字母和单词的其余部分。 It then uses a function as a callback to set the proper case.然后它使用一个函数作为回调来设置正确的大小写。

Example: http://jsbin.com/uvase示例: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 ).我应该补充一点,这根本不是帕斯卡大小写,因为您有单词障碍( helloworldhello-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".这通常被称为 Title Case,尽管它不处理诸如“FBI”、“the”或“McDonalds”之类的词。

Here's my suggestion:这是我的建议:

function toPascalCase(string) {
  return `${string}`
    .toLowerCase()
    .replace(new RegExp(/[-_]+/, 'g'), ' ')
    .replace(new RegExp(/[^\w\s]/, 'g'), '')
    .replace(
      new RegExp(/\s+(.)(\w*)/, 'g'),
      ($1, $2, $3) => `${$2.toUpperCase() + $3}`
    )
    .replace(new RegExp(/\w/), s => s.toUpperCase());
}

or或者

String.prototype.toPascalCase = function() {
  return this
    .toLowerCase()
    .replace(new RegExp(/[-_]+/, 'g'), ' ')
    .replace(new RegExp(/[^\w\s]/, 'g'), '')
    .replace(
      new RegExp(/\s+(.)(\w*)/, 'g'),
      ($1, $2, $3) => `${$2.toUpperCase() + $3}`
    )
    .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');
    chai.assert.equal(toPascalCase('Here i am'), 'HereIAm');
    chai.assert.equal(toPascalCase('FOO BAR'), 'FooBar');
  });
});

In case dash, space and other are string separators one may use lodash .如果破折号、空格和其他是字符串分隔符,可以使用lodash

eg例如

_.upperFirst(_.camelCase('double-barrel')); // => DoubleBarrel
const toPascalCase = (sentence) => sentence
   .split(' ')
   .map(word => word[0]
   .toUpperCase()
   .concat(word.slice(1)))
   .join('');

toPascalCase(words);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM