问题描述:

I have string as below

This | is a sample \| string

I would like to split the above string based on | and do not split where | is escaped. (\|).

How do I do that using javascript.

Note: I have come up with the below regular expression.

(?<!\\)\|

But Javascript does not support look behind

网友答案:

You could perform a match instead of a split:

> 'This | is a sample \\| string'.match(/([^\\|]|\\\|?)+/g)

["This ", " is a sample \| string"]

The above matches:

  1. neither a backslash nor pipe character, or
  2. if a backslash, an optional pipe character
相关阅读:
Top