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.

1
  • 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. Commented Mar 31, 2013 at 6:16

1 Answer 1

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] 
Sign up to request clarification or add additional context in comments.

2 Comments

Would it be a better option to sent it to my php file and edit it there?
Based on that answer I linked to, I'd say yes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.