交换字符串中单词的最佳方法是什么

时间:2018-04-27 18:13:58

标签: javascript string replace

如果给出以下字符串,我将如何交换左右两个字。

即。变化

  • "左" - > "右"
  • "右" - > "左"



let str = JSON.stringify({
  val: 4,
  right: {
    val: 7,
    right: { val: 9, right: null, left: null },
    left: { val: 6, right: null, left: null },
  },
  left: {
    val: 2,
    right: { val: 3, right: null, left: null },
    left: { val: 1, right: null, left: null },
  },
}, null, 2);

str = str.replace((/"left"/g), o => { return "right1" }); 
str = str.replace((/"right"/g), o => { return "left1" });

console.log(); 
console.log(str); 

str = str.replace((/"right1"/g), o => { return "right" });

console.log(); 
console.log(str); 

str = str.replace((/"left1"/g), o => { return "left" });




我正在考虑使用str.replace()。

5 个答案:

答案 0 :(得分:1)

不要字符串化(或者从字符串开始,先解析它)。使用对象本身,您可以轻松编写递归函数来执行此操作:

stringify

如果需要,您可以在最后$ azure location list data: data: Location : **canadaeast** data: DisplayName : Canada East data: Providers : Microsoft.ApiManagement, Microsoft.Batch, Microsoft.ClassicCompute, Microsoft.ClassicNetwork... ` 10:55 $ azure vm list info: Executing command vm list + Getting virtual machines data: ResourceGroupName Name ProvisioningState PowerState Location Size data: ----------------- --------------- ----------------- ---------- ----------- ----------- data: RG2FAILIMPORT importmustfail Succeeded VM running northeurope Standard_A1 data: ABIQUO-CANADAEAST abq-8e2f880d-7f Succeeded VM running **CanadaEast** Basic_A0 data: ABIQUO-CANADAEAST abq-db821ef0-b5 Succeeded VM running **CanadaEast** Basic_A0 info: vm list command OK 将其发回。

答案 1 :(得分:1)

不要使用字符串操作,保留/解析为对象并交换属性:

function swapLeftRight(obj){
  //use object deconstruction to create left and right variables, 
  //and assign them to the opposite name
  let {left:right, right:left} = obj;
  //Assign the new values
  obj.left = left, obj.right = right;
  //Use recursion if the properties are not null
  if(obj.left) swapLeftRight(obj.left);
  if(obj.right) swapLeftRight(obj.right);
}
var data = {
  val: 4,
  right: {
    val: 7,
    right: { val: 9, right: null, left: null },
    left: { val: 6, right: null, left: null },
  },
  left: {
    val: 2,
    right: { val: 3, right: null, left: null },
    left: { val: 1, right: null, left: null },
  }
};
swapLeftRight(data);
console.log(data);

答案 2 :(得分:1)

您可以传递给replace的函数的第一个参数是match

您可以更新代码以使用替换匹配" left"或"对"并翻转价值观。

(?:left|right)

let replaced = str.replace(/(?:left|right)/g, o => {
  return o === "left" ? "right" : "left";
});

请注意,使用此正则表达式,您还可以匹配" left"在" alefta"。如果您只想向左或向右匹配,可以使用单词边界\b

\b(?:left|right)\b



let str = JSON.stringify({
  val: 4,
  right: {
    val: 7,
    right: {
      val: 9,
      right: null,
      left: null
    },
    left: {
      val: 6,
      right: null,
      left: null
    },
  },
  left: {
    val: 2,
    right: {
      val: 3,
      right: null,
      left: null
    },
    left: {
      val: 1,
      right: null,
      left: null
    },
  },
}, null, 2);

let replaced = str.replace(/(?:left|right)/g, o => {
  return o === "left" ? "right" : "left";
});

console.log(replaced);




答案 3 :(得分:0)

如果您想使用replace,请使用临时值:



let str = '{"val":4,"right":{"val":7,"right":{"val":9,"right":null,"left":null},"left":{"val":6,"right":null,"left":null}},"left":{"val":2,"right":{"val":3,"right":null,"left":null},"left":{"val":1,"right":null,"left":null}}}';

function swap() {
  str = str.replace(/right/g, "temp");
  str = str.replace(/left/g, "right");
  str = str.replace(/temp/g, "left");
  console.log(str);
}

swap();




答案 4 :(得分:0)

最简单的方法是使用占位符,然后替换占位符



let str = JSON.stringify({
  val: 4,
  right: {
    val: 7,
    right: { val: 9, right: null, left: null },
    left: { val: 6, right: null, left: null },
  },
  left: {
    val: 2,
    right: { val: 3, right: null, left: null },
    left: { val: 1, right: null, left: null },
  },
}, null, 2);

let newStr = str
  .replace('right', '$')
  .replace('left', 'right')
  .replace('$', 'left')

console.log(newStr)




或者,您可以解析JSON,并递归交换节点,因为您手上实际上有一个二叉树。