有什么办法可以在texstudio中使用此脚本?

时间:2019-09-03 23:54:22

标签: javascript regex string

我想在SCRIPT中使用正则表达式替换。该代码可在texstudio中使用。

%SCRIPT
text = app.clipboard
text = text.replace(/{([^}])}{([^}])}{not}/g, "\1\2{not}")
cursor.insertText(text)

我希望{match1} {matchb} {not} {anotb}的输出为match1matchb {not} {anotb},但实际输出为{not} {anotb}。此代码在texstudio中有效。非常感谢!

2 个答案:

答案 0 :(得分:0)

将您的正则表达式更改为包含括号{},并使用replace的回调参数:

const str = "{match1}{matchb}{not}{anotb}";

const res = str.replace(/{(.*?)}{(.*?)}{not}/g, (_, m1, m2) => `${m1}${m2}{not}`);

console.log(res);

答案 1 :(得分:0)

也许,此表达式可能返回所需的输出,但是不确定:

({[^}]*not[^}]*})|{|}

替换为$1

const regex = /({[^}]*not[^}]*})|{|}/g;
const str = `{match1}{matchb}{not}{anotb}
and anything elese {match1}{matchb}{and another not field}{ and another anotb}`;
const subst = `$1`;
console.log(str.replace(regex, subst));


  

如果您想探索/简化/修改表达式,可以   在右上角的面板上进行了说明   regex101.com。如果您愿意,   也可以在this link中观看它的匹配方式   针对一些样本输入。