1

In short, like this:

> l = ['asdf', '<br>', 'lorem', 'ipsum', '<hr>', 'dollar', 'sit', 'amex']
> l.split(/<.+>/)
[
  [ 'asdf' ] ,
  [ 'lorem', 'ipsum' ] ,
  [ 'dollar', 'sit', 'amex' ]
]

I wrote join-and-split one, but it seem slow with large array.

Is there any better solutions? indexOf()?

1 Answer 1

5

A simple loop would do:

var result = [[]];
for (var i = 0; i < l.length; ++i) {
    if (/^<.+>$/.test(l[i]) {
        // start a new inner array
        result.push([]);
    } else {
        // append to the current inner array
        result[result.length-1].push(l[i]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.