Javascript Regex获取所有空格,只要它们不包含在括号中

Javascript Regex获取所有空格,只要它们不包含在括号中,javascript,regex,reactjs,ecmascript-6,regex-negation,Javascript,Regex,Reactjs,Ecmascript 6,Regex Negation,正则表达式获取所有空格,只要它们不包含在大括号中 这是一个javascript系统 例如:“说:{Joseph Empyre}{b0268efc-0002-485b-b3b0-174fad6b87fc},好吗?” 需要获得: [“说”,@:{Joseph Empyre}{b0268efc-0002-485b-b3b0-174fad6b87fc},“,”,“好的”,“对吗?” ] [编辑] 解决于: 对不起,我的英语不好我解释了你的问题,就像你对说的那样,只要空格没有用大括号括起来就可以取所有空格

正则表达式获取所有空格,只要它们不包含在大括号中

这是一个javascript系统

例如:“说:{Joseph Empyre}{b0268efc-0002-485b-b3b0-174fad6b87fc},好吗?”

需要获得:

[“说”,@:{Joseph Empyre}{b0268efc-0002-485b-b3b0-174fad6b87fc},“,”,“好的”,“对吗?” ]

[编辑]

解决于:


对不起,我的英语不好

我解释了你的问题,就像你对
说的那样,只要空格没有用大括号括起来就可以取所有空格,尽管你的结果示例不是我所期望的。您的示例结果在speak之后包含一个空格,并且在
{}
组之后为
单独匹配。下面我的输出显示了我认为您所要求的内容,一个仅在大括号外的空格上拆分的字符串列表

const str =
    "Speak @::{Joseph Empyre}{b0268efc-0002-485b-b3b0-174fad6b87fc}, all right?";

// This regex matches both pairs of {} with things inside and spaces
// It will not properly handle nested {{}}
// It does this such that instead of capturing the spaces inside the {},
// it instead captures the whole of the {} group, spaces and all,
// so we can discard those later
var re = /(?:\{[^}]*?\})|( )/g;
var match;
var matches = [];
while ((match = re.exec(str)) != null) {
  matches.push(match);
}

var cutString = str;
var splitPieces = [];
for (var len=matches.length, i=len - 1; i>=0; i--) {
  match = matches[i];
  // Since we have matched both groups of {} and spaces, ignore the {} matches
  // just look at the matches that are exactly a space
  if(match[0] == ' ') {
    // Note that if there is a trailing space at the end of the string,
    // we will still treat it as delimiter and give an empty string
    // after it as a split element
    // If this is undesirable, check if match.index + 1 >= cutString.length first
    splitPieces.unshift(cutString.slice(match.index + 1));
    cutString = cutString.slice(0, match.index);
  }
}
splitPieces.unshift(cutString);
console.log(splitPieces)
控制台:

["Speak", "@::{Joseph Empyre}{b0268efc-0002-485b-b3b0-174fad6b87fc},", "all", "right?"]

您应该扩展您的问题,使用当前代码添加对您不起作用,并提供更多的场景。我认为JavaScript的正则表达式实现可能无法实现这种类型的正则表达式。与使用正则表达式相比,您最好自己解析字符串。我相信您需要使用消极/积极的look behinds/aheads,其中一些在JavaScript中是不可能的。