当前位置: 动力学知识库 > 问答 > 编程问答 >

regex - javascript Split - Alternate to look behind

问题描述:

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
分享给朋友:
您可能感兴趣的文章:
随机阅读: