Reputation: 121
Given the following string how would I swap the words right and left.
i.e. change
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" });
I'm thinking of using str.replace().
Upvotes: 0
Views: 1180
Reputation: 163577
The first parameter of the function that you can pass to replace is the match
.
You could update your code to use an alternation to match either "left" or "right" and flip the values.
let replaced = str.replace(/(?:left|right)/g, o => {
return o === "left" ? "right" : "left";
});
Note that using this regex you would also match "left" in "alefta". If you want to match left or right only you could use a word boundary \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);
Upvotes: 1
Reputation: 45155
Don't stringify (or if starting from a string, parse it first). Work with the object itself and you can easily write a recursive function to do this:
var original = {
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 swapLR(obj) {
if (!obj || (obj.left == null && obj.right == null)) {
return obj;
}
var tmp = swapLR(obj.left);
obj.left = swapLR(obj.right);
obj.right = tmp;
return obj;
}
console.log(original);
console.log(swapLR(original));
You can then stringify
it back at the end if you need to.
Upvotes: 1
Reputation: 42746
Don't use string manipulation, keep/parse as object and swap the properties:
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);
Upvotes: 1
Reputation: 16445
The simplest way is to use a placeholder, and then replace the placeholder,
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)
Alternatively, you can parse the JSON, and recursively swap nodes because you actually have a binary tree on your hands.
Upvotes: 0
Reputation: 2377
If you want to use replace
, use a temporary value:
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();
Upvotes: 0