javascript在字符串中移动字符?

mqkwyuun  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(254)

我有一个公式,我需要移动数千个公式中的字符。这个量需要移到方程的前面,我很接近,但不知道怎么做。公式示例:

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));

这个代码打印出来 "({oval}) && ({2inchby3inch}) && {qty} >= 91000 && {qty} <=91999 && {whitebopp})) && {qty} >= 91000 && " 当我想打印的时候 "(({qty} >= 91000 && {qty} <=91999) && ({oval}) && ({2inchby3inch}) && {whitebopp}) " 我做错了什么?现在所需的文本将粘贴到末尾,我希望它粘贴到开头后一个字符。

fcipmucu

fcipmucu1#

可以使用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)

相关问题