2017-02-22 88 views
0

Angular JS中的对象数组中有几个字符串正在提取。值将看起来像us-east-1uk-west-5将字符串转换为Javascript中的混合大小写

我想将每个字符串转换为混合大小写,如US-East-1UK-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来实现。

但是有没有其他可行的解决方案?

正则表达式怎么样?有人可以解释如何使用相同的?

+1

我想正则表达式只用来发现,如果给定的字符串匹配给定的条件或没有。在这种情况下,我们不能使用regEx。我的建议是我们必须使用Angular过滤器。 –

回答

1

这应该工作。

搜索方式:

(\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 

参见:https://regex101.com/r/rzBTpo/1

相关问题