[英]How to convert camel case to proper case in Vue.js?
我正在从MongoDB集合中检索数据,并将数据填充到ag-grid中。 我希望列标题在适当情况下使用Spacing。 最初,该集合具有文本行“ businessAreaName”,我希望它看起来像“ Business Area Name”。
我正在使用正则表达式的概念,但无法找出适当情况下的表达式。
headerName: x.replace(/_/g, ' ').replace(/\w\S*/g, function (txt)
{
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
})
上面的表达式使我输出为“ Businessareaname”。 任何形式的建议,我们将不胜感激。
您真正需要做的就是
可以这样完成:
const deCamelCase = str => str.replace(/[A-Z]/g, ' $&').replace(/^./, toUppercase);
const toUppercase = str => str.toUpperCase();
分解如下:
str => str.replace(/[A-Z]/g, ' $&')
这将匹配任何大写字符(仅拉丁字母!),并将其替换为包含空格的字符串以及整个匹配的字符串,编码为$&
。 请注意,我们需要使用/g
标志来确保我们匹配每个实例。
str => str.replace(/^./, toUppercase)
这仅匹配字符串中的第一个字符,并使用toUppercase
函数替换该toUppercase
,为了便于阅读,我在一行中定义了toUppercase
函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.