JavaScript function that flips brackets direction I have these strings:
)hello(
this has ]some text[
flip }any{ brackets
even with )))]multiple[((( brackets

As you can see, the brackets are all in the wrong direction.
I have a function called flipBracketsDirection() to handle each scenario. Here is an example of input and output of the above strings:
flipBracketsDirection(')hello(');
// should return:  (hello)

flipBracketsDirection('this has ]some text[');
// should return:  this has [some text]

flipBracketsDirection('flip }any{ brackets');
// should return:  flip {any} brackets

flipBracketsDirection('even with )))]multiple[((( brackets');
// should return:  even with ((([multiple]))) brackets

Note: The direction is flipped at all times. So this is fine too:
flipBracketsDirection('flip (it) anyway');
// should return:  flip )it( anyway

Here is my solution.
function flipBracketsDirection(str: string) {
  return str
    // flip () brackets
    .replace(/\(/g, 'tempBracket').replace(/\)/g, '(').replace(/tempBracket/g, ')')

    // flip [] brackets
    .replace(/\[/g, 'tempBracket').replace(/\]/g, '[').replace(/tempBracket/g, ']')

    // flip {} brackets
    .replace(/\{/g, 'tempBracket').replace(/\}/g, '{').replace(/tempBracket/g, '}')
    ;
}

Is this the best way to create this function?
Your function seems to work fine, but you can condense it a bit by search for all bracket types in one regex replace with the following pattern:
/[\(\)\[\]\{\}]/g

and then use the replace function that takes a replace function as argument:
const brs = &quot;()({}{[][&quot;;
function flipBracketsDirection(str) {
  return str.replace(/[\(\)\[\]\{\}]/g, br =&gt; brs[brs.indexOf(br) + 1]);
}

brs holds all the replaceable brackets with the opening brackets twice, so brs[brs.indexOf(')') + 1] finds '(' as the next char in brs;
You could also let brs be an object like:
const brs =
{
  &quot;(&quot;: ')',
  &quot;)&quot;: '(',
  &quot;{&quot;: '}',
  &quot;}&quot;: '{',
  &quot;[&quot;: ']',
  &quot;]&quot;: '[',
};

and then cquery it as:
function flipBracketsDirection(str) {
  return str.replace(/[\(\)\[\]\{\}]/g, br =&gt; brs[br]);
}

Your version actually query the string nine times, where the above only iterates it once.
As an alternative to the 'sophisticated' brs dictionary-solution, you could just create a function with a switch statement:
function swapBracket(br) {
  switch (br) {
    case '(': return ')';
    case ')': return '(';
    case '{': return '}';
    case '}': return '{';
    case '[': return ']';
    case ']': return '[';
  }
}

And call that instead, this way:
function flipBracketsDirection(str: string) {
  return str.replace(/[\(\)\[\]\{\}]/g, swapBracket);
}

flipBracketsDirection('}hello{');
// will return {hello}


Your tests cases could also be simplified, so it is easier to maintain - for instance like:
  let strs = [
    ')hello(',
    'this has ]some text[',
    'flip }any{ brackets',
    'even with )))]multiple[((( brackets'
  ];

  for (let s of strs) {
    let t = flipBracketsDirection(s);
    console.log(t);
    console.log(flipBracketsDirection(t));
    console.log(&quot;&quot;);
  }


Both the above suggestions should conform to the DRY principle.