0

I'm having a problem with an array in javascript. I have this array:

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 '

As you can see, in Subjects[2], there are 2 strings

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

Also, in Subjects[3], the string starts with 'I', which is supposed to be

'Á machine lord I'.

Is there a way to cut the string where the uppercase starts and create a new index for the string? Like this:

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'

I have tried using .split with no success. Any help will be usefull.

aerojun
  • 1,206
  • 3
  • 15
  • 28
  • Easiest way: go through the string. Check whether current word has a uppercase starting symbol. Check whether next word has uppercase starting symbol. If you answer the first question with yes and the second with no, split. Repeat on the end of the string until the string is empty. – Zeta Mar 31 '13 at 06:16

1 Answers1

1

You can't (reliably) match Unicode characters using JavaScript. See: https://stackoverflow.com/a/4876569/382982

Otherwise, you could use .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]
Community
  • 1
  • 1
pdoherty926
  • 9,895
  • 4
  • 37
  • 68