2017-04-20 76 views
1

编写一个给定输入字符串的方法,反转括号内的所有字母。 例子 hellow - hellow括号内的javascript函数反转功能

H(hellow)ellow - hwollehellow

对(巴(巴兹))BLIM forbazrabblim 我盯着这个代码,但我不知道如何TDO其余

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

} 
reverseParentheses("hellow"); 
+0

什么是* “DAB tibah-输出” *?此外,这不仅适用于括号内,这反转了整个给定的字符串。 –

+0

这是“坏习惯”拼写的倒退。 –

+1

你的意思是[THIS](http://stackoverflow.com/questions/2441501/reverse-each-individual-word-of-hello-world-string-with-java)?请检查一下。谢谢。 –

回答

-1

但许多人只是downvote或标记为重复(包括我自己)这个问题。一旦问题很明显,这里就是完整的和最终的解决方案。

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(" "); 
 
}

+0

代码唯一的答案是无益的 – RobG

+0

对不起,没有打算是粗鲁的。正试图给出一个快速的答案,修正它。让我知道我应该更多地解释:) –

+0

随着最新的更新,这已不再是有效。让我解决。 –