2013-03-31 30 views
0

我在javascript中遇到数组问题。我有这样的数组:当字符为大写字符时的子串数组元素

Subjects[]: 
[0] = 'Introduction to interesting things' 
[1] = 'Cats and dogs' 
[2] = 'I can't believe it's not butter Á machine lord' 
[3] = 'I Intergalactic proton powered advertising droids ' 

正如你所看到的,在科目[2],有2串

'I can't believe it's not butter' and 'Á machine lord' 

此外,在主题[3],将字符串 'I' 开始,这应该是

'Á machine lord I'. 

有没有办法削减大写的字符串开始并创建一个新的索引的字符串?像这样:

Subjects[]: 
[0] = 'Introduction to interesting things' 
[1] = 'Cats and dogs' 
[2] = 'I can't believe it's not butter' 
[3] = 'Á machine lord I' 
[4] = 'Intergalactic proton powered advertising droids' 

我试过用.split没有成功。任何帮助将是有用的。

+0

最简单的方法:经过串。检查当前单词是否有大写起始符号。检查下一个单词是否有大写起始符号。如果你回答第一个问题是肯定的,第二个问题回答是否定的,分裂。在字符串的末尾重复,直到字符串为空。 – Zeta

回答

1

您不能(可靠地)使用JavaScript匹配Unicode字符。请参阅:https://stackoverflow.com/a/4876569/382982

否则,你可以使用.split

subjects = [ 
    'Introduction to interesting things Cats and dogs', 
    "I can't believe it's not butter Á machine lord", 
    'I Intergalactic proton powered advertising droids' 
]; 

subjects = subjects.join(' ').split(/([A-Z][^A-Z]+)/).filter(function (str) { 
    return str.length; 
}); 

// [Introduction to interesting things, Cats and dogs, I can't believe it's not butter Á machine lord, I, Intergalactic proton powered advertising droids] 
+0

将它发送到我的php文件并在那里编辑会更好吗? – aerojun

+0

根据我连接的答案,我会说是的。 – pdoherty926