1

write a method thar for a given input string, reverse all the letters inside parenthesis . examples hellow - hellow

h(hellow)ellow - hwollehellow

for(bar(baz))blim forbazrabblim i stared with this code, but i don't know how to tdo the rest

function reverseParentheses(phrase) {
    return phrase.split('').reverse().join('');

}
reverseParentheses("hellow");
Cœur
  • 37,241
  • 25
  • 195
  • 267
Gerardo Leon
  • 61
  • 1
  • 1
  • 4

1 Answers1

-1

Yet many just downvote or marked this question as a duplicate (including myself). Once the question was much clear here is the full and final Solution.

var inputText = "this is a (bad habit) and reverse (todo odot)";
var regex = /\(([^)]+)\)/g;

alert(reverseParentheses(inputText, regex));

// Reverse Text Within Parentheses
function reverseParentheses(input, pattern){
  result = input;
  while (match = pattern.exec(input)) {
      var replaceFrom = match[0];

      // If you don't want parentheses in your final output remove them here.
      var replaceTo = "(" + reverseWords(match[1]) + ")";
      var result = result.replace(replaceFrom, replaceTo);
  }
  
  return result;
}

// Reverse Words
function reverseWords(phrase) {
    return phrase.split("").reverse().join("").split(" ").reverse().join(" ");
}
Randi Ratnayake
  • 674
  • 9
  • 23