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 '

如您所见,在 Subjects[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没有成功。任何帮助都会很有用。

4

1 回答 1

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]
于 2013-03-31T07:12:15.863 回答