使用{{Text}}格式Javascript拆分文本

时间:2018-06-01 07:07:34

标签: javascript regex

抱歉打扰你们。我不知道正则表达式。但是现在我需要一个非常糟糕的。 我想使用此格式{{Text}}拆分文本。 “文字”可以是任何东西。我只需要在{{Text}}的位置拆分文本。

以下是一个示例。

var Regx = My Regx;
var String = "{{This}} is a {{test}} string to be {{spliced}} with {{Regular}} Expression";

var SplitArray = String.split(Regx);
// it will give me an array like this
// ["","is a ","string to be "," with"," Expression"]

提前谢谢。

修改 我自己也解决了。它是{{[^{}]+}}

2 个答案:

答案 0 :(得分:1)

你可以这样做

var test = "{{This}} is a {{test}} string to be {{spliced}} with {{Regular}} Expression";
var SplitArray = test.split(/\{\{.*?\}\}/);

console.log(SplitArray)

答案 1 :(得分:0)

试试这个:



var Regx = /\{\{.*?\}\}/;
var String = "{{This}} is a {{test}} string to be {{spliced}} with {{Regular}} Expression";

var SplitArray = String.split(Regx);
console.log(SplitArray);
// it will give me an array like this
// ["","is a ","string to be "," with"," Expression"]