char为大写时的子串数组元素

时间:2013-03-31 05:49:13

标签: javascript

我在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'

此外,在Subjects [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但没有成功。任何帮助都是有用的。

1 个答案:

答案 0 :(得分: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]