JS regexp基于不带反斜杠的字符来拆分字符串

use*_*159 1 javascript regex string node.js

我想使用JS String split函数仅根据逗号分割此字符串,,而不是以反斜杠开头的逗号/,.我怎样才能做到这一点?

'this,is\,a,\,string'.split(/,/)
Run Code Online (Sandbox Code Playgroud)

这段代码将它拆分为所有字符串,我不知道如何让它只在不带反斜杠的逗号上拆分.

Dmi*_*rov 5

由于JavaScript不支持lookbehinds,因此很难为split分配"not preceded something"模式.但是,您可以将"单词"定义为非逗号或转义逗号的序列:

(?:\\,|[^,])+
Run Code Online (Sandbox Code Playgroud)

(演示:https://regex101.com/r/d5W21v/1)

并提取所有"单词"匹配:

var matches = "this,is\\,a,\\,string".match(/(?:\\,|[^,])+/g);
console.log(matches);
Run Code Online (Sandbox Code Playgroud)