Split string in JavaScript using RegExp ignoring the delimiter inside brackets

后端 未结 2 961
名媛妹妹
名媛妹妹 2020-12-06 20:45

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

2条回答
  •  情深已故
    2020-12-06 21:20

    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.

提交回复
热议问题