Javascript 移动字符串中的字符?

Javascript move characters in string?

我有一个公式,我需要在数千个公式中移动字符。数量需要移到等式的前面,我很接近但无法弄清楚如何。 公式示例:

const formula = '(({oval}) && ({2inchby3inch}) && {qty} >= 91000  && {qty} <=91999  && {whitebopp})';
var index1 = (formula.indexOf("{qty}")) - 5;
var index2 = (formula.indexOf("&& {whitebopp}")) - 15;
console.log(formula.slice(1)+formula.slice(index1, index2));

当我想要打印 "(({qty} >= 91000 && {qty} <=91999) && ({oval}) && ({2inchby3inch}) && {whitebopp}) " 时,这段代码会打印出来 "({oval}) && ({2inchby3inch}) && {qty} >= 91000 && {qty} <=91999 && {whitebopp})) && {qty} >= 91000 && " 我究竟做错了什么?现在想要的文字粘贴在末尾,我想在开头后粘贴一个字符。

您可以使用 split() 和 reduce() 重新排列数组

let formula = '(({oval}) && ({2inchby3inch}) && {qty} >= 91000  && {qty} <=91999  && {whitebopp})';
formula = formula.slice(1, -1)
let nformula = "(" + formula.split(" && ").reduce((b, a) => a.includes('{qty}') ? [a, ...b] : [...b, a], []).join(" && ") + ")"
console.log(nformula)