【问题标题】:Javascript capitalize string with hyphenJavascript用连字符大写字符串
【发布时间】:2021-10-19 23:36:05
【问题描述】:

我有一个字符串数组,想把它大写。 已循环到字符串数组,并希望将连字符后的第一个字母和字母大写并删除连字符

locales= ['es-US', 'en-US', 'en-CA', 'fr-CA', 'ar', 'fi', 'hy']

for(const st of locales) {
   const basePkg = `locale${st.replace(/(-.)/g,function(x){return x[1].toUpperCase()})}`;
   console.log(basePkg); // I get output as ['localeesUS', 'localeenUS', 'localeenCA', 'localefrCA', 'localear', 'localefi', 'localehy'] 
}

需要的输出应该是

['localeEsUS', 'localeEnUS', 'localeEnCA', 'localeFrCA', 'localeAr', 'localeFi', 'localeHy'] 

任何帮助将不胜感激

【问题讨论】:

    标签: javascript arrays angular typescript object


    【解决方案1】:

    您可以为第一个字符添加replace

    st.replace(/(-.)/g, x => x[1].toUpperCase()).replace(/^./, x => x.toUpperCase())
    

    例子:

    const locales = ['es-US', 'en-US', 'en-CA', 'fr-CA', 'ar', 'fi', 'hy']
    
    console.log(locales.map(st => `locale${st.replace(/(-.)/g, x => x[1].toUpperCase()).replace(/^./, x => x.toUpperCase())}`));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-05
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-07
      • 2010-09-26
      相关资源
      最近更新 更多