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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…