将字符串转换为Javascript中的混合大小写
问题描述:
Angular JS中的对象数组中有几个字符串正在提取。值将看起来像us-east-1
或uk-west-5
。将字符串转换为Javascript中的混合大小写
我想将每个字符串转换为混合大小写,如US-East-1
或UK-West-5
。
我知道我可以通过splitting the strings by - and then to convert to toUpperCase() for the first item in array and then to convert only the first letter of the second item item in the array
来实现。
但是有没有其他可行的解决方案?
正则表达式怎么样?有人可以解释如何使用相同的?
答
这应该工作。
搜索方式:
(\w+-)(\w)(\w+)
替换:
\U$1\U$2\L$3
说明:
\U => Uppercase
\L => Lowercase
输入:
us-east-1 or uk-west-5
输出:
US-East-1 or UK-West-5
我想正则表达式只用来发现,如果给定的字符串匹配给定的条件或没有。在这种情况下,我们不能使用regEx。我的建议是我们必须使用Angular过滤器。 –