2013-03-31 92 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