4

I have various instances of strings that I need to split. Following are some examples and the desired output scenarios. The rules to split are also listed:

Example 1:

input: 'filename.ext|someattributes'
output array: 
  'filename.ext', 
  'someattributes'

Example 2:

input: qualifier1[filename.ext|someattributes]|qualifier2[another_filename.ext|some_other_attributes]
output array: 
  'qualifier1[filename.ext|someattributes]',
  'qualifier2[another_filename.ext|some_other_attributes]'

Example 3:

input: dummyqualifier|qualifier1[filename.ext|someattributes]
output array: 
  'dummyqualifier', 
  'qualifier1[filename.ext|someattributes]'

The rules are simple. Split the string using '|' as delimiter only when it does not appear inside square brackets. Note: The string may not have any square brackets. There are no spaces in the input strings.

I am looking for a solution in JavaScript as this is for a node.js module.

2 Answers 2

3

This regex should work for the cases you've outlined:

/\|(?!(?:\w+\|?)+])/

Here's an example of it running: http://jsfiddle.net/UFq3h/1/ (you will need to have the console opened to see the results).

Crude explanation: any | character not followed by (word characters or | followed by ]). If you need a more precise explanation post a comment and I'll try to make it clearer.

Edit: Thanks to Lolo for the improved version, which handles the last example in the use case.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks draevor. This works great. I think I understand how it is working.
1

I can't think of regex right now, but you can do this:

function doSplit(input) {
    var tmp = input.split('|');

    var result = [];
    for (var i = 0, j = 0; i < tmp.length; i++) {
        result[j] = (result[j] ? result[j] + '|' : '') + tmp[i];
        if (result[j].indexOf('[') == -1 || result[j].indexOf(']') != -1) {
            j++;
        }
    }

    return result;
}

var i = 'qualifier1[filename.ext|someattributes]|qualifier2[another_filename.ext|some_other_attributes]';
var o = doSplit(i);

1 Comment

Thanks Lolo, appreciate your quick response.

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.