flat149
flat149

Reputation: 21

regular expression to split string avoiding double tokens

In order to split a string value into an array using javascript I need to split using delimiters. Repeated delimiters indicate a sub-value within the array, so for example

abc+!+;def+!+!+;123+!+;xyz

should split into abc, [def, 123], xyz

My nearest expression is ((?:+!(?!+!))+\;|$) but thinking about it that may be the one I first started with, as I've gone through so many many variations since then.

There is probably a blindingly obvious answer, but after what seems an eternity I'm now stumped. I took a look at regex to parse string with escaped characters, and similar articles which were close although not the same problem, but basically came to a stop with ideas.

Somewhere out there someone will know regular expressions far better than I do, and hope that they have an answer

Upvotes: 1

Views: 115

Answers (1)

Sam
Sam

Reputation: 20486

I got this to work by using .split() with this basic pattern:

\b\+!\+;\b

And then:

\b\+!\+!\+;\b

And so on, and so forth. You will need to turn this into a recursive function, but I made a basic JSFiddle to get you started. First we split the string using our first expression. Then we create our newer expression by adding !\+ (this can easily be done dynamically). Now we can loop through our initial array, see if the string matches our new expression and if it does split it again.

var string = 'abc+!+;def+!+!+;123+!+;xyz',
    data = string.split(/\b\+!\+;\b/);

var regex = /\b\+!\+!\+;\b/
for(var i = 0; i < data.length; i++) {
    var string = data[i];

    if(string.match(regex)) {
        data[i] = string.split(regex);
    }
}

console.log(data);
// ["abc", ["def", "123"], "xyz"]

I'm leaving the task of making this a recursive function up to OP. If you want some direction, I can try to provide some more insight.

Upvotes: 1

Related Questions