将camelcase字符串拆分为单词单词数组 [英] split camelcase string into array of words words

查看:80
本文介绍了将camelcase字符串拆分为单词单词数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在寻找一个最佳的javascript函数来拆分一个camelcase

字符串并返回一个数组。


我想可以循环遍历字符串,检查字符是否为
大写并开始构建一个新单词以添加到数组但是

看起来非常浪费。必须是一些简单的方法。

谢谢。

Hi,

I''m looking for an optimal javascript function to split a camelcase
string and return an array.

I suppose one could loop through the string, check if character is
uppercase and start building a new word to add to the array but that
seems incredibly wasteful. must be some easy way to do it.
Thanks.

推荐答案



" pantagruel" < ra ************* @ gmail.comwrote in message

news:11 ***************** *****@i3g2000cwc.googlegro ups.com ...

"pantagruel" <ra*************@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...




我在寻找一个最佳的javascript函数来拆分一个camelcase

字符串并返回一个数组。


我想可以循环遍历字符串,检查字符是否为

大写并开始构建一个新单词以添加到数组但是
似乎非常浪费。必须有一些简单的方法来做到这一点。
Hi,

I''m looking for an optimal javascript function to split a camelcase
string and return an array.

I suppose one could loop through the string, check if character is
uppercase and start building a new word to add to the array but that
seems incredibly wasteful. must be some easy way to do it.



这是一个有趣的小早晨脑温暖。 ; ^)


我已经创建了一个功能,我认为,它会做你想要的。这不是很好,但它看起来很有效。在我的例子中,我已将它添加到String原型

(使其可用于所有字符串)但你可以把它放在你喜欢的任何地方...

你也可以将它压缩到一行代码并将其附加到任何

字符串但不要这样做:函数应该用于抽象丑陋的代码

像这样。 ; ^)


这是:


String.prototype.CamelCaseToArray = function(){

/ / preceed大写(或多组)用逗号删除任何

领先逗号

var Delimed = this.replace(/([AZ] +)/ g," ,

This was a fun little morning brain warmer. ;^)

I''ve created a function that, I think, will do what you want. It''s not that
pretty but it seem to work. In my example I''ve added it to String prototype
(making it available to all strings) but you can put it anyplace you like...
you could also condense it down to one line of code and append it to any
string but don''t do that: functions should be used to abstract ugly code
like this. ;^)

Here it is:

String.prototype.CamelCaseToArray = function() {
// Preceed Uppercase (or sets of) with commas then remove any
leading comma
var Delimed = this.replace(/([A-Z]+)/g, ",


1")。replace(/ ^,/,"");

//在逗号上拆分字符串并返回数组

返回Delimed.split(",");

};


基本上这是一个正则表达式搜索大写字母(或

套大写字母)并以逗号开头。由于字符串

现在可能有逗号,因为它的第一个字符是另一个替换完成

删除它(如果它存在)。然后整个混乱被分成逗号并且

返回。


这里有一个如何使用它的例子:


TestString =" MyCamelCASEString";


alert(TestString.CamelCaseToArray());


此函数将(我希望很明显)如果

中有逗号开头的字符串有问题。我已经用一把琴弦对它进行了测试,但是在你信任它之前,你需要更多地测试它。


我想(因为我只是关心寻找大写字符)它应该幸福地被数字和标点符号(

逗号除外)所困扰。


希望这会有所帮助,


Jim Davis
1").replace(/^,/, "");
// Split the string on commas and return the array
return Delimed.split(",");
};

Basically this does a regular expression search for uppercase letters (or
sets of uppercase letters) and preceeds them with commas. Since the string
may now have a comma as it''s first character another replace is done to
remove it (if it exists). Then the whole mess is split on the commas and
returned.

Here''s an example of how you might use it:

TestString = "MyCamelCASEString";

alert(TestString.CamelCaseToArray());

This function will (I hope obviously) have problems if there are commas in
the string to begin with. I''ve tested it on a handful of strings but you
should vette it more before trusting it.

I think (since I''m only caring to look for uppercase characters) that it
should be blissfully unfazed by numbers and punctuation (other than the
comma).

Hope this helps,

Jim Davis


pantagruel写道:
pantagruel wrote:




我正在寻找一个最佳的javascript函数来拆分一个camelcase

字符串并返回一个数组。


我想可以循环遍历字符串,检查字符是否为
大写并开始构建一个新单词以添加到数组但是

似乎非常浪费。必须是一些简单的方法。


谢谢。
Hi,

I''m looking for an optimal javascript function to split a camelcase
string and return an array.

I suppose one could loop through the string, check if character is
uppercase and start building a new word to add to the array but that
seems incredibly wasteful. must be some easy way to do it.
Thanks.



" CamelCaseString" .match(/ [AZ] [az] * / g)


返回数组:


(" Camel"," Case"," String") 。


看起来像你想要的:-)


Jeremy

"CamelCaseString".match(/[A-Z][a-z]*/g)

returns array:

("Camel", "Case", "String").

Seems like what you want :-)

Jeremy


这篇关于将camelcase字符串拆分为单词单词数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆