我有我需要拆分的各種字符串實例。以下是一些示例和所需的輸出方案。規則來分割也列出:在JavaScript中使用RegExp拆分字符串,忽略括號內的分隔符
實施例1:
input: 'filename.ext|someattributes'
output array:
'filename.ext',
'someattributes'
實施例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]'
實施例3:
input: dummyqualifier|qualifier1[filename.ext|someattributes]
output array:
'dummyqualifier',
'qualifier1[filename.ext|someattributes]'
規則很簡單。使用'|'分割字符串僅當它不出現在方括號內時才作爲分隔符。注意:該字符串可能沒有任何方括號。輸入字符串中沒有空格。
我正在尋找JavaScript解決方案,因爲這是針對node.js模塊的。
謝謝draevor。這很好。我想我明白它是如何工作的。 –